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

给大家一个正确的代码,供不过者参考

Posted by zpdlut at 2009-02-19 16:56:02 on Problem 1251
#include<iostream>
using namespace std;
void prim(int input[26][26],int n)
{
	int sum=0,min,i,k;
	int s[26]={0},lowcost[26];
	s[0]=1;
	for(i=1;i<n;i++)
		lowcost[i]=input[0][i];
	for(i=0;i<n-1;i++)
	{
		min=101;
		int j=0;
		for(k=1;k<n;k++)
		{
			if(!s[k]&&lowcost[k]<min)
			{
				min=lowcost[k];
				j=k;
			}
		}
		sum+=min;
		s[j]=1;
		for(k=1;k<n;k++)
		{
			if(!s[k]&&input[j][k]<lowcost[k])
				lowcost[k]=input[j][k];
		}
	}
	cout<<sum<<endl;
}
int main()
{
	int input[26][26],start,end;
	int num,n;
	char ch1;
	while(cin>>num&&num!=0)
	{
		memset(input,101,sizeof(input));
		for(int i=0;i<num-1;i++)
		{
			cin>>ch1>>n;
			start=ch1-'A';
			for(int j=0;j<n;j++)
			{
				cin>>ch1;
				end=ch1-'A';
				cin>>input[start][end];
				input[end][start]=input[start][end];
			}
		}
		prim(input,num);
	}

		return 1;
}

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