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

一次AC 水过 prim

Posted by Heng_Xing at 2011-07-22 11:01:48 on Problem 1287
#include<stdio.h>
#define INT_MAX 0x7fffffff
int v[51][51];
int p,r;
int min,max;
void prim(int ve)
{
	int i,j,k;
	int d[51];
	int f[51];
	for(i=1;i<=p;i++)
	{
		d[i]=v[ve][i];
		f[i]=0;
	}
	f[ve]=1;
	for(i=1;i<p;i++)
	{
		min=INT_MAX;
		for(j=1;j<=p;j++)
		{
			if(!f[j]&&d[j]<min)
			{
				min=d[j];
				k=j;
			}
		}
		f[k]=1;
		if(min<INT_MAX)
			max+=min;
		for(j=1;j<=p;j++)
		{
			if(!f[j]&&v[k][j]<INT_MAX&&v[k][j]<d[j])
				d[j]=v[k][j];
		}
	}
}
int main (void)
{
	int i,j;
	int a,b,w;
	while(scanf("%d",&p),p!=0)
	{
		max=0;
		scanf("%d",&r);
		for(i=1;i<=p;i++)
			for(j=1;j<=p;j++)
				v[i][j]=INT_MAX;
		for(i=1;i<=r;i++)
		{
			scanf("%d %d %d",&a,&b,&w);
			if(v[a][b]>w)
				v[a][b]=v[b][a]=w;
		}
		prim(1);
		printf("%d\n",max);
	}
	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