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

我用kruskal做的。为什么错啊。。。有什么边界数据吗?

Posted by huxinjie800 at 2009-08-20 21:11:32 on Problem 1679
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
	int x,y,w,ii;
};
node a[10005];
int cmp (node a,node b)
{
	return a.w<b.w;
}
int flag[105],n;
node ans[105];
int findcircle(node a)
{
	int temp,temp2,i;
	if(flag[a.x]==flag[a.y]&&flag[a.x]!=0)return 0;
	else 
	{
		if(flag[a.x]==flag[a.y]&&flag[a.x]==0)flag[a.x]=flag[a.y]==a.ii;
		else
		{
			temp=flag[a.x]<flag[a.y]?flag[a.x]:flag[a.y];
			temp2=flag[a.x]>flag[a.y]?flag[a.x]:flag[a.y];
			for(i=0;i<n;i++)if(flag[i]==temp2)flag[i]=temp;
		}
		return 1;
	}
}
int main ()
{
	int t,m,i,j,k,ff,temp,top,sum,sum1,count;
	scanf("%d",&t);
	while(t--)
	{
		ff=0;
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++)flag[i]=0;
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
			a[i].ii=i;
		}
		sort(a,a+m,cmp);
		sum=0;
		top=0;
		count=1;
		for(i=0;count<=n-1;i++)
		{
			if(findcircle(a[i]))
			{
				ans[top++]=a[i];
				sum+=a[i].w;
				count++;
			}
		}
		for(k=0;k<top;k++)
		{
			for(i=0;i<n;i++)flag[i]=0;
			sum1=0;
			temp=ans[k].ii;
			count=1;
			for(i=0;count<=n-1;i++)
			{
				if(findcircle(a[i])&&a[i].ii!=temp)
				{
					sum1+=a[i].w;
					count++;
				}
			}
			if(sum==sum1){ff=1;break;}
		}
		if(ff==1)printf("Not Unique!\n");
		else printf("%d\n",sum);
	}
	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