Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这道题目WA了N次了,求高手指点啊

Posted by 201041842103 at 2012-09-10 10:24:02 on Problem 2421
//权值非负,顶点编号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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator