-
Notifications
You must be signed in to change notification settings - Fork 0
/
1406.cpp
58 lines (58 loc) · 880 Bytes
/
1406.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
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char a[2],b[2];
int m,cnt,root[101];
struct edge
{
int x,y,l;
friend bool operator<(edge a,edge b)
{
return a.l<b.l;
}
}e[100];
int getroot(int x)
{
if(x==root[x])
return x;
return root[x]=getroot(root[x]);
}
int main()
{
while(scanf("%d",&n),n)
{
cnt=0;
for(int i=1;i<=n;i++)
root[i]=i;
for(int i=1;i<=n-1;i++)
{
scanf("%s",a);
int k;
scanf("%d",&k);
for(int j=1;j<=k;j++)
{
scanf("%s",b);
int c;
scanf("%d",&c);
e[++cnt].x=*a-'A'+1;
e[cnt].y=*b-'A'+1;
e[cnt].l=c;
}
}
sort(e+1,e+cnt+1);
int sum=0;
for(int i=1;i<=cnt;i++)
{
int x=e[i].x,y=e[i].y;
x=getroot(x);
y=getroot(y);
if(x==y)
continue;
root[x]=y;
sum+=e[i].l;
}
printf("%d\n",sum);
}
return 0;
}