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 |
这道题目WA了N次了,求高手指点啊//权值非负,顶点编号1-n #include<stdio.h> #include<string.h> #define MAXV 101 #define MAXINT 999999999 int g[MAXV][MAXV],vis[MAXV],dis[MAXV],p[MAXV]; void initial() { int i,j; for(i=0;i<MAXV;i++) { vis[i]=0; dis[i]=MAXINT; p[i]=-1; g[i][i]=MAXINT; } } int prim(int nvertex,int start) { int v,i,distance; dis[start]=0; v=start; while(!vis[v]) { vis[v]=1; for(i=1;i<=nvertex;i++) { if(dis[i]>g[v][i]) { dis[i]=g[v][i]; p[i]=v; } } distance=MAXINT; for(i=1;i<=nvertex;i++) { if(!vis[i] && dis[i]<distance) { distance=dis[i]; v=i; } } } distance=0; for(i=1;i<=nvertex;i++) { if(p[i]!=-1) distance+=g[p[i]][i]; } return distance; } int main() { int i,j,n,m,a,b,t; while(EOF!=scanf("%d",&n)) { initial(); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&t); if(i!=j) g[i][j]=t; } } scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); g[a][b]=g[b][a]=0; } printf("%d\n",prim(n,1)); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator