Conscription--POJ-NO.3723 最小生成树

Description

Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have a relationship d and one of them has been collected, Windy can collect the other one with 10000-d RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.

Input

The first line of input is the number of test case.
The first line of each test case contains three integers, N, M and R.
Then R lines followed, each contains three integers xi, yi and di.
There is a blank line before each test case.

1 ≤ N, M ≤ 10000
0 ≤ R ≤ 50,000
0 ≤ xi < N
0 ≤ yi < M
0 < di < 10000

Output

For each test case output the answer in a single line.

Sample Input:

2

5 5 8
4 3 6831
1 3 4583
0 0 6592
0 1 3063
3 3 4975
1 3 2049
4 2 2104
2 2 781

5 5 10
2 4 9820
3 2 6236
3 1 8864
2 4 8326
2 0 5156
2 0 1463
4 1 2439
0 4 4373
3 4 8889
2 4 3133

Sample Output:

71071
54223

Kruskal:

源代码

#include<algorithm>
#include<stdio.h> 
#include<queue>
using std::priority_queue;
class DisjSet
{
private:
	int* disjSet;
	int parts;

public:
	DisjSet(const int max_size)
	{
		disjSet = new int[max_size];
		for (int i = 0; i < max_size; ++i)
			* (disjSet + i) = i;
		parts = max_size;
	}
	int getParts() {
		return parts;
	}
	int find(int x) {
		return x == *(disjSet + x) ? x : (*(disjSet + x) = find(*(disjSet + x)));
	}
	void to_union(int x1, int x2) {
		if (find(x1) != find(x2)) {
			*(disjSet + find(x1)) = find(x2);
			--parts;
		}
	}
	bool is_same(int e1, int e2) {
		return find(e1) == find(e2);
	}
	~DisjSet()
	{
		delete[] disjSet;
		disjSet = NULL;
	}
};
struct Edge
{
	int from;
	int to;
	int cost;
	Edge(int from = 0, int to = 0, int cost = 0) {
		this->from = from;
		this->to = to;
		this->cost = cost;
	}
	bool operator < (const Edge& edge2) const {
		return this->cost > edge2.cost;
	}
};
int main() {
	int n = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int N = 0;
		int M = 0;
		int nn = 0;
		scanf("%d", &N);
		scanf("%d", &M);
		scanf("%d", &nn);
		priority_queue<Edge> edge;
		DisjSet disjset(M + N);
		for (int i = 0; i < nn; i++) {
			Edge e;
			scanf("%d %d %d", &e.from, &e.to, &e.cost);
			e.to += N;
			e.cost = 10000 - e.cost;
			edge.push(e);
		}
		long long money = 0;
		while (!edge.empty())
		{
			Edge e = edge.top();
			edge.pop();
			if (!disjset.is_same(e.from, e.to)) {
				money += e.cost;
				disjset.to_union(e.from, e.to);
			}
		}
		money += static_cast<long long>(disjset.getParts()) * 10000;
		printf("%lld\n", money);
	}
	return 0;
}

Prim_普通实现

超内存:

#include<algorithm>
#include<utility>
#define MAX 10000
#define min(x,y) x<y?x:y;
int main() {
	int n = 0;
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++) {
		int money = 0;
		int N = 0;
		int M = 0;
		int nn = 0;
		scanf_s("%d", &N);
		scanf_s("%d", &M);
		scanf_s("%d", &nn);
		int* mincost = new int[M + N];
		bool* used = new bool[M + N];
		for (int j = 0; j < M + N; j++)
		{
			mincost[j] = MAX;
			used[j] = false;
		}
		int** cost = new int* [M + N];
		for (int i = 0; i < M + N; i++)
		{
			cost[i] = new int[M + N];
			for (int j = 0; j < M + N; j++)
			{
				cost[i][j] = MAX;
			}
		}
		for (int i = 0; i < nn; i++) {
			int x = 0;
			int y = 0;
			int value = 0;
			scanf_s("%d %d %d", &x, &y, &value);
			value = MAX - value;
			cost[y + N][x] = min(cost[y+N][x],value);
			cost[x][y + N] = cost[y + N][x];
		}
		while (true)
		{
			int v = -1;
			for (int i = 0; i < M + N; i++)
			{
				if (!used[i] && (v == -1 || mincost[i] < mincost[v])) {
					v = i;
				}
			}
			if (v == -1)
				break;
			used[v] = true;
			money += mincost[v];
			for (int i = 0; i < M + N; i++)
			{
				if (mincost[i]>cost[v][i]) {
					mincost[i] = cost[v][i];
				}
			}
		}
		printf_s("%d\n", money);
		delete[] used;
		delete[] mincost;
		for (int i = 0; i < M + N; i++)
		{
			delete[] cost[i];
		}
		delete[] cost;
	}
	return 0;
}

Prim_优先队列实现_超时:

#include<stdio.h> 
#include<algorithm>
#include<vector>
#include<utility>
#include<queue>
#include<set>
using std::priority_queue;
using std::vector;
using std::pair;
using std::set;
#define MAX 10000
int main() {
	int n = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int money = 0;
		int N = 0;
		int M = 0;
		int nn = 0;
		scanf("%d", &N);
		scanf("%d", &M);
		scanf("%d", &nn);
		set<int> unused;
		for (int j = 0; j < M + N; j++)
			unused.insert(j);
		priority_queue<pair<int, int>, vector<pair<int, int> >, std::greater<pair<int, int> > > * G 
			= new priority_queue<pair<int, int>, vector<pair<int, int> >, std::greater<pair<int, int> > >[M + N];
		for (int i = 0; i < nn; i++) {
			int x = 0;
			int y = 0;
			int value = 0;
			scanf("%d %d %d", &x, &y, &value);
			value = MAX - value;
			G[x].push(std::make_pair(value, y + N));
			G[y + N].push(std::make_pair(value, x));
		}
		priority_queue<pair<int, int>, vector<pair<int, int> >, std::greater<pair<int, int> > > que;
		while (unused.size())
		{
			que.push(std::make_pair(MAX ,*(unused.begin())));
			while (!que.empty()) {
				pair<int, int> p = que.top();
				que.pop();
				if (unused.find(p.second)==unused.end())
					continue;
				money += p.first;
				unused.erase(p.second);
				while (!G[p.second].empty()) {
					if (unused.find(G[p.second].top().second) != unused.end())
						que.push(G[p.second].top());
					G[p.second].pop();
				}
			}
		}
		printf("%d\n", money);
	}
	return 0;
}

令人窒息的操作… … … … … … … … … … … … … … …
白搞了几天
最初只有最初做出来那个是正确的… … … … … … … …

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值