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啊!求前辈们,大侠们,路过的看下代码指点一下啊!kruskal捉的。。。编译器都试过了。。。

Posted by huronghai at 2012-08-12 22:20:13 on Problem 1258 and last updated at 2012-08-12 22:20:52
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int Max=501;
int f[Max];
struct node{
	int u,v,w;
}edge[50000];
int n;
bool cmp(node a,node b)
{
	return a.w<=b.w?true:false;
}
int find(int x)
{
	if(x!=f[x])
		f[x]=find(f[x]);
	return f[x];
}
void Union(int x,int y)
{
	f[x]=y;
}
 __int64 kruskal(int m)
{
	__int64 cost=0;
	sort(edge,edge+m,cmp);
	for(int i=0;i<m;i++){
		int fx=find(edge[i].u);
		int fy=find(edge[i].v);
		if(fx!=fy){
			cost+=edge[i].w;
			Union(fx,fy);
		}
	}
	return cost;
}
int main()
{
	int x,m,a,b;
	while(scanf("%d",&n)!=EOF){
		m=0;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++){
				scanf("%d",&x);
				if(i<j){
					edge[m].u=i;
					edge[m].v=j;
					edge[m].w=x;
					m++;
				}
			}
			for(int i=0;i<=m;i++)
				f[i]=i;
			printf("%I64d\n",kruskal(m));
	}
	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