【图论】POJ-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.path).
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
二、思路&心得
三、代码
#include<cstdio>
#include<algorithm>
#define MAX_V 20005
#define MAX_E 50005
using namespace std;
int fa[MAX_V];
int N, M, R, t;
struct Edge {
int a;
int b;
int cost;
} E[MAX_E];
bool cmp(Edge x, Edge y) {
return x.cost < y.cost;
}
void Union_init(int n) {
for(int i = 0; i <= n; i ++)
fa[i] = i;
}
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
void Union(int x, int y) {
x = find(x);
y = find(y);
if(x != y) fa[x] = y;
}
int same(int x, int y) {
return find(x) == find(y);
}
int Kruscal() {
int x, y, sum = 0;
for(int i = 0; i < R; i ++) {
x = E[i].a, y = E[i].b;
x = find(x), y = find(y);
if( x != y ) {
sum += E[i].cost;
fa[x] = y;
}
}
return sum;
}
void solve() {
scanf("%d %d %d", &N, &M, &R);
Union_init(N + M + 1);
for (int i = 0; i < R; i ++) {
scanf("%d %d %d", &E[i].a, &E[i].b, &E[i].cost);
E[i].b += N, E[i].cost = -E[i].cost;
}
sort(E, E + R, cmp);
printf("%d\n", 10000 * (N + M) + Kruscal());
}
int main() {
scanf("%d", &t);
while(t --) {
getchar();
solve();
}
return 0;
}
【图论】POJ-3723 最大生成树的更多相关文章
- POJ 3723 Conscription MST
http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...
- poj - 3723 Conscription(最大权森林)
http://poj.org/problem?id=3723 windy需要挑选N各女孩,和M各男孩作为士兵,但是雇佣每个人都需要支付10000元的费用,如果男孩x和女孩y存在亲密度为d的关系,只要他 ...
- POJ 3723 Conscription(并查集建模)
[题目链接] http://poj.org/problem?id=3723 [题目大意] 招募名单上有n个男生和m个女生,招募价格均为10000, 但是某些男女之间存在好感,则招募的时候, 可以降低与 ...
- POJ 3723 Conscription 最小生成树
题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...
- POJ 3723
最大生成树 #include<iostream> #include<cstdio> #include<cstring> #include<set> #i ...
- POJ 3723 Tree(树链剖分)
POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...
- MST:Conscription(POJ 3723)
男女搭配,干活不累 题目大意:需要招募女兵和男兵,每一个人都的需要花费1W元的招募费用,但是如果有一些人之间有亲密的关系,那么就会减少一定的价钱,如果给出1~9999的人之间的亲密关系,现在要你求 ...
- Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...
- 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...
- POJ 3723 Conscription
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6325 Accepted: 2184 Desc ...
随机推荐
- React-Native使用极光进行消息推送
推送作为APP几乎必备的功能,不论是什么产品都免不了需要消息推送功能,一般做RN开发的可能都是前端出身(比如我),关于android ios 都不是很懂
- Scala_类和对象
类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象是具体的,占用存储空间. import scala.beans.BeanProperty class ChecksumAccumul ...
- VB6 加载水晶报表例子
VB6 加载水晶报表例子 先按照水晶报表组件 Crystal Reports,Business Objects,现已被SAP收购. 再添加引用 'Library: CRAXDRT 'C:\Progra ...
- c++ 变量共享内存-联合(union)
共享内存极少使用,所以这里我们仅作了解. .将几个变量放在相同的内存区,但其中只有一个变量在给定时刻有有效值. .程序处理许多不同类型的数据,但是一次只处理一种.要处理的类型在执行期间才能确定. .在 ...
- 4 jdngo vue-rotue
. 执行linux命令 与mac命令 git base 3 初始化操作 页面加载完成,发起请求 window.onload
- [WC2011]最大XOR和路径 线性基
[WC2011]最大XOR和路径 LG传送门 需要充分发掘经过路径的性质:首先注意不一定是简单路径,但由于统计的是异或值,重复走是不会被统计到的,考虑对于任意一条从\(1\)到\(n\)的路径的有效部 ...
- React——JSX
一.将表达式嵌套在JSX中 要在JSX中内嵌js表达式只需要将js表达式放在{}中,例如: const element = <h1>this is a JSX {sayName()}< ...
- 微信小程序日记(一)
一.基础知识(目录与配置) (1)标签 小程序的view相当于HTML的div标签一样,作占位 (2)每一个页面都需要在app.json里面注册,例如: { { "pages": ...
- TPO-22 C2 Revise a music history paper
第 1 段 1.Listen to part of a conversation between a student and his music history professor. :听一段学生和音 ...
- Laya自动图集原理
关于Laya自动图集 Laya会把size小于512*512的图片打入自动大图集中.如果图片被打入自动图集中,图片的内存就交由Laya自动处理,开发者不能手动删除. Laya最多生成6张2048*20 ...