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 |
好一瓶纯净水!!!!!附代码!!!#include<stdio.h> #include<string.h> #define I_NF 1000000 int dis[666][666],mark[666]; int prim(int K) { int d[666],i,j,n,l; for(i=0;i<K;i++) d[i]=dis[0][i]; mark[0]=1; int s=0; for(i=1;i<K;i++) { int min=I_NF+5; int Record=-5; for(j=0;j<K;j++) { if(d[j]<min&&mark[j]==0) { min=d[j]; Record=j; } } if(Record<0) break; s+=min; mark[Record]=1; for(j=0;j<K;j++) { if(!mark[j]&&dis[Record][j]<d[j]) { d[j]=dis[Record][j]; } } } return s; } int main() { int N,nCase; while(~scanf("%d",&N)) { if(N==0) break; scanf("%d",&nCase); int i,j,n,m; for(i=0;i<N;i++) { for(j=0;j<N;j++) dis[i][j]=I_NF; mark[i]=0; } for(i=0;i<nCase;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); { if(dis[a-1][b-1]>c) { dis[a-1][b-1]=c; dis[b-1][a-1]=c; } } } printf("%d\n",prim(N)); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator