-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path最小生成树.cpp
66 lines (61 loc) · 950 Bytes
/
最小生成树.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
char aa,bb;
int wei;
} *nodever;
int* ver,nver;
int main()
{
void creat();
void show();
creat();
show();
return 0;
}
void creat()
{
cin>>nver;
ver=new int[nver*2];
for(int i=0;i<nver*2;i++)
{
ver[i]=i;
}
nodever=new node[nver];
for(int i2=0;i2<nver;i2++)
{
char aa,bb;
int wei;
cin>>aa>>bb>>wei;
nodever[i2]={aa,bb,wei};
}
}
void show()
{
int sum=0;
bool mysort(node a,node b);
sort(nodever,nodever+nver,mysort);
for(int i=0;i<nver;i++)
{
if(ver[nodever[i].aa-'A']!=ver[nodever[i].bb-'A'])
{
cout<<nodever[i].aa<<' '<<nodever[i].bb<<' '<<nodever[i].wei<<endl;
sum+=nodever[i].wei;
int tndst=ver[nodever[i].bb-'A'];
int tnori=ver[nodever[i].aa-'A'];
for(int i2=0;i2<nver;i2++)
{
if(ver[i2]==tnori)
ver[i2]=tndst;
}
}
}
cout<<sum;
system("PAUSE");
}
bool mysort(node a,node b)
{
return a.wei<b.wei;
}