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

前一题还是用prim做的AC了 ,这题就是简单的prim,测了多组数据了,就是过不了,帮我看看,多谢!

Posted by oboaix at 2008-03-15 22:08:04 on Problem 1258
#include <iostream>
#include <stdio.h>

using namespace std;

double edge[101][101],key[101];

int findmin(double *a, int n)
{
	int i,j;
	double min=10000000;
	for(i=1;i<=n;i++)
	{
		if((a[i]!=0)&&(a[i]<min))
		{
			min=a[i];
			j=i;
		}
	}
	return j;
}

void main()
{
	int i,j,k,n;
	double sum;
	while(cin>>n)
	{
		sum=0;
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			{
				cin>>edge[i][j];
			}
		for(i=1;i<=n;i++)
		{
			key[i]=edge[1][i];
		}
		key[1]=0;
		for(i=2;i<=n;i++)
		{
			j=findmin(key,n);
			sum+=key[j];
			key[j]=0;
			for(k=2;k<=n;k++)
			{
				if(edge[j][k]<key[k])
					key[k]=edge[j][k];
			}
		}
		cout<<sum<<endl;
	}
}

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