标签:des style blog class code java
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 12717 Accepted
Submission(s): 5506
最小生成树入门题:
1 //531MS 272K 965B 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 int st; 40 while(scanf("%d",&n)!=EOF && n) 41 { 42 for(int i=0;i<=n;i++) set[i]=i; 43 for(int i=0;i<n*(n-1)/2;i++){ 44 scanf("%d%d%d%d",&p[i].u,&p[i].v,&p[i].d,&st); 45 if(st==1) merge(p[i].u,p[i].v); 46 } 47 qsort(p,n*(n-1)/2,sizeof(p[0]),cmp); 48 printf("%d\n",kruskal()); 49 } 50 return 0; 51 }
hdu 1879 继续畅通工程 (最小生成树),布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/GO-NO-1/p/3710314.html