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

为什么WA啊?郁闷

Posted by ericlee at 2007-04-24 17:49:05 on Problem 2421
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
struct EDGES
{
	int from;
	int to;
	int w;
}edges[5051];
int p[101];
int cmp(const void* a ,const void* b)
{
	struct EDGES* l = (EDGES*) a;
	struct EDGES* r = (EDGES*) b;
	return l->w - r->w;
}
int root(int x)
{
	int r = x;
	while(p[r]!=r)
		r = p[r];
	while(x!=r)
	{
		int k = p[x];
		p[x] = r;
		x = k;
	}
	return r;
}
int main()
{
	int i,j,k,n,m,c=0,q,ret=0;
	for(i=1; i<101; i++)
		p[i] = i;

	scanf("%d", &n);
	for(i=1; i<=n; i++)
		for(j=1; j<=n; j++)
		{
			scanf("%d", &m);
			if(j>i)
			{
				edges[++c].from = i;
				edges[c].to = j;
				edges[c].w = m;
			}
		}

	qsort(edges+1, c, sizeof(EDGES), cmp);
	
	scanf("%d", &q);
	for(i=1; i<=q; i++)
	{
		scanf("%d %d", &j , &k);
		int x = root(j);
		int y = root(k);
		if(x!=y)
			p[j] = k; //union;
	}
	
	//kruskal
	for(i=1; i<=c; i++)
	{
		int x = root(edges[i].from);
		int y = root(edges[i].to);
		if(x!=y)
		{
			p[x] = y;
			ret += edges[i].w;
		}
	}

	printf("%d\n", ret);
	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