-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1251Jungle Roads(最小生成树).cpp
77 lines (77 loc) · 1.09 KB
/
POJ1251Jungle Roads(最小生成树).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
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int inf=0x3f3f3f3f,MAXN=30;
int father[MAXN];
struct edge
{
int u,v,w;
} e[MAXN*MAXN];
bool cmp(edge x,edge y)
{
return x.w<y.w;
}
void init(int n)
{
for(int i=1;i<=n;i++)
father[i]=i;
}
int findfa(int x)
{
if(father[x]!=x)
father[x]=findfa(father[x]);
return father[x];
}
int Merge(int a,int b)
{
int x=findfa(a),y=findfa(b);
if(x==y)
return 0;
else if(x<y)
father[y]=x;
else
father[x]=y;
return 1;
}
int kruskal(int n,int m)//
{
int ans=0;
for(int i=0;i<m;i++)
{
if(Merge(e[i].u,e[i].v))
{
ans+=e[i].w;
n--;
if(n==1)
return ans;
}
}
return 0;
}
int main()
{
int n,k,cnt=0;
char ch,chpre;
while(cin>>n&&n){
init(n);
cnt=0;
for(int i=1;i<n;i++)
{
cin>>chpre>>k;
int x=chpre-'A'+1,y;
for(int j=0;j<k;j++)
{
cin>>ch>>e[cnt].w;
y=ch-'A'+1;
e[cnt].u=x,e[cnt].v=y;
cnt++;
}
}
sort(e,e+cnt,cmp);
cout<<kruskal(n,cnt)<<endl;
}
return 0;
}