标签:des style blog class code java
Time Limit: 4000/2000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 24027 Accepted
Submission(s): 10674
最小生成树入门题:
1 //281MS 272K 889B G++ 2 #include<stdio.h> 3 #include<string.h> 4 #include<stdlib.h> 5 struct node{ 6 int u,v; 7 int d; 8 }p[10005]; 9 int set[105],n; 10 int cmp(const void*a,const void*b) 11 { 12 return (*(node*)a).d-(*(node*)b).d; 13 } 14 int find(int x) 15 { 16 if(x!=set[x]) set[x]=find(set[x]); 17 return set[x]; 18 } 19 int merge(int a,int b) 20 { 21 int x=find(a); 22 int y=find(b); 23 if(x!=y){ 24 set[x]=y; 25 return 1; 26 } 27 return 0; 28 } 29 int kruskal() 30 { 31 int ans=0; 32 for(int i=0;i<n*(n-1)/2;i++) 33 if(merge(p[i].u,p[i].v)) 34 ans+=p[i].d; 35 return ans; 36 } 37 int main(void) 38 { 39 while(scanf("%d",&n)!=EOF && n) 40 { 41 for(int i=0;i<=n;i++) set[i]=i; 42 for(int i=0;i<n*(n-1)/2;i++) 43 scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].d); 44 qsort(p,n*(n-1)/2,sizeof(p[0]),cmp); 45 printf("%d\n",kruskal()); 46 } 47 return 0; 48 }
hdu 1233 还是畅通工程 (最小生成树),布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/GO-NO-1/p/3710290.html