标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24307 | Accepted: 11430 |
Description
Input
Output
Sample Input
9 A 2 B 12 I 25 B 3 C 10 H 40 I 8 C 2 D 18 G 55 D 1 E 44 E 2 F 60 G 38 F 0 G 1 H 35 H 1 I 35 3 A 2 B 10 C 40 B 1 C 20 0
Sample Output
216 30
Source
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 int read(){ 9 int x=0,f=1;char ch=getchar(); 10 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 11 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 12 return x*f; 13 } 14 const int mxn=30; 15 int n; 16 struct node{ 17 int x,y; 18 int dis; 19 }e[mxn*mxn];int cnt=0; 20 int cmp(node a,node b){ 21 return a.dis<b.dis; 22 } 23 int fa[mxn]; 24 void init(){ 25 for(int i=1;i<=n;i++)fa[i]=i; 26 } 27 int find(int x){ 28 if(fa[x]==x)return x; 29 return fa[x]=find(fa[x]); 30 } 31 int ans=0; 32 void Kruskal(){ 33 int i,j; 34 int ti=0; 35 ans=0; 36 for(i=1;i<=cnt;i++){ 37 int u=find(e[i].x),v=find(e[i].y); 38 if(u==v)continue; 39 ti++; 40 ans+=e[i].dis; 41 fa[u]=v; 42 if(ti==n-1)break; 43 } 44 return; 45 } 46 int main(){ 47 while(scanf("%d",&n) && n){ 48 init(); 49 cnt=0; 50 char ch[5]; 51 int u,v,dis;int m; 52 int i,j; 53 for(i=1;i<n;i++){ 54 scanf("%s%d",&ch,&m); 55 u=(ch[0]-‘A‘)+1; 56 while(m--){ 57 scanf("%s%d",&ch,&dis); 58 v=ch[0]-‘A‘+1; 59 e[++cnt]=(node){u,v,dis}; 60 } 61 } 62 sort(e+1,e+cnt+1,cmp); 63 Kruskal(); 64 printf("%d\n",ans); 65 } 66 return 0; 67 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5890918.html