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

Re:什么叫做有重边啊?

Posted by majiaof at 2007-12-02 21:08:53 on Problem 2377
In Reply To:Re:什么叫做有重边啊? Posted by:TOTOT at 2007-11-29 22:27:41
下面是我的程序,用prim做的。
重边也考虑到了啊,为什么会wa?

#include<iostream>
using namespace std;

int main()
{
	int i,j,k;
	int n,m;
	int temp,totalcost=0,count=1;
	int cost[101][101];
	bool mark[1010];
	cin>>n>>m;
	int *MST=new int [n];
	memset(cost,0,sizeof(cost));
	memset(mark,0,sizeof(mark));
	for(i=0;i<m;i++)
	{
		cin>>j>>k>>temp;
		if(temp<cost[j][k]) continue;
		cost[k][j]=cost[j][k]=temp;
	}
	MST[0]=1;
	mark[1]=1;
	int max=0,flag;
	while(count<n)
	{
		max=0;
		i=MST[count-1];
		for(j=1;j<=n;j++)
			if(cost[i][j]>0 && mark[j]==0)
			{
				if(cost[i][j]>max)
				{
					max=cost[i][j];
					flag=j;
				}
			}
		if(max==0)
		{
			cout<<-1<<endl;
			return 0;
		}
		mark[flag]=1;
		MST[count]=flag;
		totalcost+=cost[i][flag];
		count++;
	}
	cout<<totalcost<<endl;
	delete MST;
	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