【图论】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 ...
随机推荐
- Redis 可视化工具 Redis Desktop Manager 和 treeNMS 的使用
这里介绍两个 Redis 可视化工具.Redis Desktop Manager 和 treeNMS. 一.Redis Desktop Manager 下载地址:https://redisdeskto ...
- 生死系列--WuJie
WuJie,高中和中专时期的同学,篮球队的队友. 在高三时认识的,我们隔壁班的,但仅限于认识,并未打过交道.高中毕业后考在同一所学校,同一个班,象棋下的很好,喜欢打扑克牌,在班上任团支部书记. 球队时 ...
- 关于DP和背包
听说过动态规划(DP)的同学应该都知道有背包问题的存在. 首先我们来了解一下动态规划 基本思想: 动态规划算法通常用于求解具有某种最优性质的问题. 在这类问题中, 可能会有很多可行解.每一个解都对应于 ...
- windows7下解决caffe check failed registry.count(type) == 1(0 vs. 1) unknown layer type问题
在Windows7下调用vs2013生成的Caffe静态库时经常会提示Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer t ...
- Oracle保存带&的数据
在SQL*Plus中默认的"&"表示替代变量,也就是说,只要在命令中出现该符号,SQL*Plus就会要你输入替代值.这就意味着你无法将一个含有该符号的字符串输入数据库或赋给 ...
- c3p0 连接数据库失败的问题
报错:java.sql.SQLException: No suitable driver 显然,这里c3p0由于没有找到对应的配置文件(c3p0-config.xml或者c3p0.properties ...
- JavaScript函数作用域与对象以及实用技巧
1. JS作用域 1.1 全局作用域和局部作用域 函数外面声明的就是 全局作用域 函数内是局部作用域 全局变量可以直接在函数内修改和使用 变量,使用var是声明,没有var是使用变量. 如果在函数内使 ...
- python模块-datetime模块
上面一篇已经讲了time模块,再来学习datetime模块. datetime主要有datetime.timedelta.time.date这4个子模块. a.datetime常用的函数(dateti ...
- GNU构建系统和Autotool
原文:http://os.51cto.com/art/201609/518191.htm 经常使用Linux的开发人员或者运维人员,可能对configure->make->make ins ...
- AxWebBrowser 实现的多进程浏览器 (一)
我们使用 C#/VB.NET 进行 Trident 内核浏览器编程,大多都是单进程的,当打开的页面较多时比较容易出现卡死等情况. 单进程浏览器简单示例: Public Class formBrowse ...