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 smwwh at 2010-09-23 12:04:52 on Problem 2421
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define NMAX 105
struct Edge {
	int s, e, v;
	bool operator<(const Edge& e) const {
		return v < e.v;
	}
}edge[NMAX * NMAX];
int N, Q, cur;
int vset[NMAX];
int kruskal()
{
	int i, j, sum = 0, V1, V2;
	j = 0;
	while(cur < (N - 1))
	{
		V1 = edge[j].s;
		V2 = edge[j].e;
		if(vset[V1] != vset[V2])
		{
			sum += edge[j].v;
			cur++;
			for(i=1; i<=N; i++)
				if(vset[i] == vset[V2])
					vset[i] = vset[V1];
		}
		j++;
	}
	return sum;
}
int main() {
//freopen("in.txt", "r", stdin);
	int i, j, r, k;
	while(scanf("%d", &N) != EOF) {
		for(i=1; i<=N; i++)
			vset[i] = i;
		r = 0;
		k = 1;
		for(i=1; i<=N; i++) {
			++k;
			for(j=1; j<k; j++)
				scanf("%*d");
			for(; j<=N; j++) {
				edge[r].s = i;
				edge[r].e = j;
				scanf("%d", &edge[r++].v);
			}
		}
		scanf("%d", &Q);
		int a, b;
		cur = 0;
		while(Q--) {
			scanf("%d%d", &a, &b);
			if(vset[a] != vset[b]) {
				for(i=1; i<=N; i++)
					if(vset[i] == vset[a])
						vset[i] = vset[b];
				cur++;
			}
		}
		sort(edge, edge + r);
		printf("%d\n", kruskal());
	}
	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