标签:des style blog http color os java io strong
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1879
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14075 Accepted Submission(s):
6136
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 struct node 6 { 7 int s,d,f,g; 8 } p[5000]; 9 10 int father[110],sum,k; 11 12 void set(int n) 13 { 14 for (int i=1; i<=n; i++) 15 father[i]=i; 16 } 17 18 bool cmp(const node &a,const node &b) 19 { 20 return a.f<b.f; 21 } 22 23 int find(int a) 24 { 25 if (father[a]==a)return a; 26 return father[a]=find(father[a]); 27 } 28 29 void Union(int x,int y,int z) 30 { 31 x=find(x); 32 y=find(y); 33 if (x!=y) 34 { 35 sum+=z; 36 father[x]=y; 37 k++; 38 } 39 } 40 41 int main () 42 { 43 int n; 44 while (scanf("%d",&n),n) 45 { 46 sum=0;k=0; 47 set(n); 48 for (int i=1; i<=n*(n-1)/2 ; i++) 49 { 50 scanf("%d%d%d%d",&p[i].s,&p[i].d,&p[i].f,&p[i].g); 51 if(p[i].g==1) 52 p[i].f=0; 53 } 54 sort(p+1,p+n*(n-1)/2+1,cmp); 55 for (int i=1; i<=n*(n-1)/2; i++) 56 { 57 Union(p[i].s,p[i].d,p[i].f); 58 if(k==n) 59 break; 60 } 61 printf ("%d\n",sum); 62 } 63 return 0; 64 }
标签:des style blog http color os java io strong
原文地址:http://www.cnblogs.com/qq-star/p/3938208.html