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

用priority queue又写了一个,还WA,奇了怪了,跪求测试数据或指点代码

Posted by love_mely at 2012-12-27 11:58:00 on Problem 1258
#include<iostream>
#include<queue>
using namespace std;
class Edge
{
public:
	int u;
	int v;
	int w;
	friend bool operator < (Edge  e1, Edge  e2)
	{
		return e1.w>e2.w;
	}
};
const int inf=100001;
int parent[110];
int n;
priority_queue<Edge> pq;
int find(int k)
{
	return parent[k];
}
void unions(int i,int j)
{
	for(int k=0;k<n;k++)
		if(parent[k]==j)
			parent[k]=parent[i];
}
int kruskal()
{
	long int sum=0;
	int k=0;
	for(int i=0;i<n;i++)
		parent[i]=i;
	while(k < n-1)
	{
		Edge e=pq.top();
		pq.pop();
		int u=e.u;
		int v=e.v;
		if(find(u)!=find(v))
		{
			unions(u,v);
			sum+=e.w;
			k++;
		}
	}
	return sum;
}
int main(void)
{
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
			{
				int w;
				cin>>w;
				if(j > i)
				{
					Edge e;
					e.u=i;
					e.v=j;
					e.w=w;
					pq.push(e);
				}
			}
		cout<<kruskal()<<endl;
	}
	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