-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoj1154.cpp
More file actions
67 lines (62 loc) · 1.08 KB
/
oj1154.cpp
File metadata and controls
67 lines (62 loc) · 1.08 KB
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
67
#include <stdio.h>
#include <algorithm>
using namespace std;
int tree[26];
void treeInit(){
for(int i=0;i<26;i++)
tree[i]=-1;
return;
}
int findRoot(int x){
if(tree[x]==-1)
return x;
int tmp=findRoot(tree[x]);
tree[x]=tmp;
return tmp;
}
struct Edge{
int a,b;
int cost;
void init(){
a=-1;
b=-1;
cost=1000;
}
bool operator < (const Edge &A) const{
return cost<A.cost;
}
} edge[100];
int main(){
int n;
while(scanf("%d",&n)&&n){
treeInit();
for(int i=0;i<100;i++)
edge[i].init();
int edgeSize=0;
for(int i=0;i<n-1;i++){
char start[3],end[3];
int a,b,k,cost;
scanf("%s%d",start,&k);
a=start[0]-'A';
for(int j=0;j<k;j++){
scanf("%s%d",end,&cost);
b=end[0]-'A';
edge[edgeSize].a=a;
edge[edgeSize].b=b;
edge[edgeSize++].cost=cost;
}
}
sort(edge,edge+edgeSize);
int ans=0;
for(int i=0;i<edgeSize;i++){
int a=findRoot(edge[i].a);
int b=findRoot(edge[i].b);
if(a!=b){
ans+=edge[i].cost;
tree[a]=b;
}
}
printf("%d\n",ans);
}
return 0;
}