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怎么老WA?

Posted by alex900420 at 2008-11-14 21:11:42 on Problem 1251
#include <stdio.h>
#define INTREE 0
struct edge 
{
	int head;
	int tail;
	int cost;
} e[80];

int tree[27];

int main()
{
	int n, i, j, k, cost, t, min, len;
	char str1[2], str2[2];
	while (scanf("%d",&n) && n!=0)
	{
		for (i=1;i<=27;i++)
		{
			tree[i] = i;
		}
		t = 0;
		for (i=1;i<=n-1;i++)
		{
			scanf("%s %d",&str1,&k);
			for (j=1;j<=k;j++)
			{
				scanf("%s %d",&str2,&cost);
				e[++t].head = str1[0] - 'A' + 1;
				e[t].tail = str2[0] - 'A' + 1;
				e[t].cost = cost;
			}
		}
		k = 0;
		len = 0;
		while (k < n-1)
		{
			min = 100;
			for (i=1;i<=t;i++)
			{
				if ((min >= e[i].cost) && (tree[e[i].head] != tree[e[i].tail]))
				{
					min = e[i].cost;
					j = i;
				}
			}
			len += min;
			if (k == 0)
			{
				tree[e[j].head] = INTREE;
				tree[e[j].tail] = INTREE;
			} 
			else
			{
				for (i=1;i<=2000;i++)
				{
					if (tree[i] == tree[e[j].head] || tree[i] == tree[e[j].tail])
					{
						tree[i] = ((tree[e[j].head])<=(tree[e[j].tail]))?(tree[e[j].head]):(tree[e[j].tail]);
					}
				}
			}
			k++;
		}
		printf("%d\n",len);
	}
	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