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

虽然边是按boy-girl输入的,但我们可以把其统一起来,对一类端点加个偏移即可

Posted by uuuouou at 2014-11-14 10:49:34 on Problem 3723
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAX_NODE	10005
#define MAX_EDGE	50005

const int Price = 10000;

int N, M, R;
struct Edge{
	int x, y, d;
	bool  operator < (const Edge& other)const{
		return d > other.d;
	}
} edge[MAX_EDGE];
int father[MAX_NODE * 2];
int Find(int x){
	return father[x] != x ? father[x] = Find(father[x]) : x;
}

void build()
{
	scanf("%d%d%d", &N, &M, &R);
	for(int i = 0; i < R; ++i){
		scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].d);
		edge[i].y += N;//add a BIAS to girl's id, which is the boy's count
	}
}
int kruskal()
{
	//initialize
	for(int i = M+N-1; i >= 0; --i) father[i] = i;
	//sort the edges by descending order
	sort(edge, edge + R);
	//select, there might be isolate nodes though
	int total = 0, k = M+N-1;
	for(int i = 0; i < R && k; ++i){
		int fx = Find(edge[i].x), fy = Find(edge[i].y);
		if(father[fx] != father[fy]){
			--k;
			total += edge[i].d;
			father[fy] = fx;
		}
	}
	return (M+N) * Price - total;
}


int main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);

	int test;
	for(scanf("%d", &test); test--; ){
		build();
		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