标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17840 Accepted Submission(s): 7682
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int per[110]; 6 struct node 7 { 8 int b,e,v; 9 }s[5050]; 10 bool cmp(node x,node y) 11 { 12 return x.v<y.v; 13 } 14 void init() 15 { 16 for(int i=0;i<110;i++) 17 per[i]=i; 18 } 19 int find(int x) 20 { 21 while(x!=per[x]) 22 x=per[x]; 23 return x; 24 } 25 bool join (int x,int y) 26 { 27 int fx=find(x); 28 int fy=find(y); 29 if(fx!=fy) 30 { 31 per[fx]=fy; 32 return true; 33 } 34 return false; 35 } 36 37 38 int main() 39 { 40 int n,i,flag; 41 while(scanf("%d",&n),n) 42 { 43 init(); 44 int m=n*(n-1)/2; 45 for(i=0;i<m;i++) 46 { 47 scanf("%d%d%d%d",&s[i].b,&s[i].e,&s[i].v,&flag); 48 if(flag)//最主要的就是把修过的路的价值清0 49 s[i].v=0; 50 } 51 sort(s,s+m,cmp); 52 int sum=0; 53 for(i=0;i<m;i++) 54 { 55 if(join(s[i].b,s[i].e)) 56 sum+=s[i].v; 57 } 58 printf("%d\n",sum); 59 } 60 return 0; 61 }
标签:
原文地址:http://www.cnblogs.com/Eric-keke/p/4721208.html