-
Notifications
You must be signed in to change notification settings - Fork 0
/
45JungleRoads1154.cpp
66 lines (65 loc) · 1.27 KB
/
45JungleRoads1154.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>
#include<cmath>
#include<cstring>
using namespace std;
char tree[27];
int FindRoot(int x)
{
if(tree[x]==-1)
return x;
else
{
int tmp=FindRoot(tree[x]);
tree[x]=tmp;
return tmp;
}
}
class Edge
{
public:
char a,b;//村庄的名字是大写的字母
int cost;
bool operator <(const Edge &e)const
{
return cost<e.cost;
}
} edge[20];
int main()
{
int n;
while(cin>>n&&n!=0)
{
for(int i=1; i<=n; i++)
{
tree[i]=-1;
}
int siz=0;//记录有多少条边
for(int i=0; i<n-1; i++)
{
char label;
int num;
cin>>label>>num;
for(int j=0; j<num; j++)
{
edge[siz].a=label;//与第一条边相互关联的边都写进来,用一个双层循环
cin>>edge[siz].b>>edge[siz].cost;
siz++;
}
}
sort(edge,edge+siz);
int ans=0;
for(int i=0; i<siz; i++)
{
int a=FindRoot(int(edge[i].a-64));
int b=FindRoot(int(edge[i].b-64));
if(a!=b)
{
tree[a]=b;
ans+=edge[i].cost;
}
}
cout<<ans<<endl;
}
return 0;
}