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 |
为什么TLM#include<stdio.h> int const INF=1000000; void init(int map[30][30],int len) { int i,j; for(i=0;i<len;i++) for(j=0;j<len;j++) map[i][j]=INF; } int prim(int map[][30],int len) { int i,j,min,index,sum=0; int mst[30]; int weigh[30]; for(i=0;i<len;i++) { mst[i]=1; weigh[i]=map[0][i]; } mst[0]=0; for(i=1;i<len;i++) { min=INF; for(j=1;j<len;j++) { if(weigh[j]<min && mst[j]) { min=weigh[j]; index=j; } } sum+=min; mst[index]=0; for(j=0;j<len;j++) { if(map[index][j]<weigh[j]) weigh[j]=map[index][j]; } } return sum; } int main() { int k,sum,num,n,m,i,j,index; int map[30][30]; char ch; while(scanf("%d",&n)!=EOF && n) { init(map,n); for(k=0;k<n-1;k++) { getchar(); ch=getchar(); scanf("%d",&m); index=ch-'A'; for(i=0;i<m;i++) { getchar(); ch=getchar(); scanf("%d",&num); j=ch-'A'; map[index][j]=num; map[j][index]=num; } } sum=prim(map,n); printf("%d\n",sum); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator