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

小心是多个CASE的,附小弟一边泡妞一边AC的代码,哈哈

Posted by donkeyinacm at 2010-02-01 20:09:58 on Problem 1258
#include <iostream>
#define INF 9999999
#define MAX_VERTEX_NUM 110
struct 
{
	int adjvex;
	int lowcost;
}closeedge[MAX_VERTEX_NUM];
int N;
int G[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

int Prim(int u)
{
	int res=0;
	for(int j=0;j<N;j++)
		if(j!=u)
		{
			closeedge[j].adjvex=u;
			closeedge[j].lowcost=G[u][j];
		}
		closeedge[u].lowcost=0;
		for(int j=1;j<N;j++)
		{
			int mini=-1,minv=INF;
			for(int i=0;i<N;i++)
				if(minv>closeedge[i].lowcost&&closeedge[i].lowcost!=0)
				{
					mini=i;
					minv=closeedge[i].lowcost;
				}
				res+=minv;
				closeedge[mini].lowcost=0;
				for(int i=0;i<N;i++)
					if(closeedge[i].lowcost&&closeedge[i].lowcost>G[mini][i])
					{
						closeedge[i].adjvex=mini;
						closeedge[i].lowcost=G[mini][i];
					}
		}
		return res;
}

int main()
{
	//freopen("c:/aaa.txt","r",stdin);
	while(scanf("%d",&N)!=EOF)
	{
	for(int r=0;r<N;r++)
		for(int c=0;c<N;c++)
			scanf("%d",&G[r][c]);
	int res=Prim(0);
	printf("%d\n",res);
	}
	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