Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用scanf("%s%d"),printf可以过的#include <stdio.h> #include <string.h> #include <stdio.h> int v1[80],v2[80],e[80],r[80],p[27]; int find(x) { return (p[x] == x) ? x : (p[x] = find(p[x])); } int compare(const void *e1,const void *e2) { int *p1 = (int *)e1; int *p2 = (int *)e2; return e[*p1] - e[*p2]; } int kruskal(int vi,int ei) { int i,x,y,tmp,ans; int count = 0; for(i = 0;i < vi;i++) p[i] = i; ans = 0; for(i = 0;i < ei;i++) { tmp = r[i]; x = find(v1[tmp]); y = find(v2[tmp]); if(x != y) { p[x] = y; ans += e[tmp]; count++; } if(count == vi - 1) break; } return ans; } int main() { // FILE *fp; //fp = fopen("d:\\c.txt","r"); int i,n,t,k,w; char cs[2],ce[2]; scanf("%d",&n); //fscanf(fp,"%d",&n); while(n) { t = 0; for(i = 0;i < n-1;i++) { scanf("%s%d",cs,&k); //fscanf(fp,"%s%d",cs,&k); { while(k--) { //scanf(fp,"%s%d",ce,&w); scanf("%s%d",ce,&w); e[t] = w; v1[t] = cs[0] - 'A'; v2[t] = ce[0] - 'A'; t++; } } } for(i = 0;i < t;i++) r[i] = i; qsort(r,t,sizeof(r[0]),compare); printf("%d\n",kruskal(n,t)); scanf("%d",&n); //fscanf(fp,"%d",&n); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator