标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30823 Accepted Submission(s): 13828
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
3 5
1 #include <stdio.h> 2 #include <algorithm> 3 int father[110]; 4 using namespace std; 5 6 struct vilage 7 { 8 int a,b,c; 9 }; 10 vilage num[5000]; 11 12 bool cmp(vilage a, vilage b) 13 { 14 return a.c < b.c ; 15 } 16 17 int find(int a) 18 { 19 if(father[a]!=a) 20 father[a] = find(father[a]) ; 21 return father[a] ; 22 } 23 24 int mercy (int a, int b) 25 { 26 a=find(a) ; 27 b=find(b) ; 28 if(find(a)!=find(b)) 29 { 30 father[a] = b ; 31 return 1; 32 } 33 else 34 return 0; 35 } 36 37 int main() 38 { 39 int n; 40 while(~scanf("%d",&n),n) 41 { 42 int i; 43 for(i=1; i<=n; i++) 44 father[i]=i ; 45 46 n=n*(n-1)/2 ; 47 for(i=1;i<=n;i++) 48 scanf("%d %d %d",&num[i].a, &num[i].b, &num[i].c); 49 sort(num+1,num+n+1,cmp); 50 51 int total=0 ; 52 for(i=1; i<=n; i++) 53 { 54 if(mercy(num[i].a, num[i].b)) 55 total+=num[i].c ; 56 } 57 printf("%d\n",total); 58 } 59 return 0; 60 }
//模板题 ;
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4612357.html