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

为什么TLM

Posted by B08310208 at 2009-09-19 22:10:35 on Problem 1251
#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:
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