POJ3723 Conscription 【并检查集合】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8071 | Accepted: 2810 |
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
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
Source
题意:有N个女孩M个男孩去报名,每一个人报名花费10000,可是假设未报名的小孩跟已报名的小孩中有关系亲热的异性,那么能够少花一些钱。
给出若干男女关系之间的1~9999亲热度,报名费用为10000-(已报名的人中跟自己亲热度的最大值)。求全部人的报名费用和的最小值。
题解:不能由于看到男女关系就朝二分图想,实际上这题用的是最小生成树思想。虽然最后的结果可能是森林,仅仅需让ans+=森林个数*10000;
#include <stdio.h>
#include <string.h>
#include <algorithm> #define maxn 20010
#define maxm 100010
#define COST 10000 int N, M, R, id;
int pre[maxn];
struct Node {
int u, v, w;
} E[maxm]; bool cmp(Node a, Node b) {
return a.w < b.w;
} void addEdge(int u, int v, int w) {
E[id].u = u; E[id].v = v; E[id++].w = w;
} void getMap() {
int x, y, d; id = 0;
while(R--) {
scanf("%d%d%d", &x, &y, &d);
addEdge(x, y + N, COST - d);
}
} int ufind(int k) {
int a = k, b;
while(pre[k] != -1) k = pre[k];
while(a != k) {
b = pre[a];
pre[a] = k;
a = b;
}
return k;
} bool same(int x, int y) {
return ufind(x) == ufind(y);
} void unite(int x, int y) {
x = ufind(x);
y = ufind(y);
if(x != y) pre[y] = x;
} void Kruskal() {
int cnt = N + M, i, x, y, ans = 0;
memset(pre, -1, sizeof(int) * (N + M));
std::sort(E, E + id, cmp);
for(i = 0; i < id; ++i) {
if(!same(E[i].u, E[i].v)) {
unite(E[i].u, E[i].v);
ans += E[i].w;
if(--cnt == 1) break;
}
}
printf("%d\n", ans + COST * cnt);
} int main() {
// freopen("stdin.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &N, &M, &R); // G B
getMap();
Kruskal();
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
POJ3723 Conscription 【并检查集合】的更多相关文章
- HDU 1272 小希迷宫(并检查集合)
意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...
- hdu1325 Is It A Tree?并检查集合
pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...
- URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)
意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...
- CodeForces 277A Learning Languages (并检查集合)
A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- zoj 3659 并检查集合
http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...
- uva 11987 Almost Union-Find (并检查集合)
标题效果: 三操作. 1. 合并两个集合 2.代替所述第二组的第一个元素 3.输出设置数量,并.. IDEAS: 使用p该元素的记录数,其中集合,建立并查集. #include <cstdio& ...
- 《算法导论》2.3-7 检查集合中是否存在两数字和为指定的X--算法和证明
习题2.3-7:设计一个算法,对于一个给定的包含n个整数的集合S和另一个给定的整数X,该算法可以在时间内确定S中是否存在两个元素,使得它们的和恰为X. 解题思路:首先应该想到的是先用一个的排序算法对S ...
- POJ3723 Conscription
http://poj.org/problem?id=3723 这题虽然简单,但是还是错了很多次. 因为这题构建的图可能是不连通的.也就是说可能有很多棵树. 所以我以前写的并查集用在这上面会出问题的. ...
随机推荐
- js进阶 12-2 彻底弄懂JS的事件冒泡和事件捕获
js进阶 12-2 彻底弄懂JS的事件冒泡和事件捕获 一.总结 一句话总结:他们是描述事件触发时序问题的术语.事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件.相反的,事件 ...
- Intellij IDEA中使用Debug
Intellij IDEA中使用Debug Debug用来追踪代码的运行流程,通常在程序运行过程中出现异常,启用Debug模式可以分析定位异常发生的位置,以及在运行过程中参数的变化.通常我们也可以启用 ...
- JavaScript的Math对象
原文 简书原文:https://www.jianshu.com/p/8776ec9cfb58 大纲 前言 1.Math对象的值属性 2.Math对象的函数属性 3.Math对象的函数的使用 前言 Ma ...
- 美轮美奂宇宙星空制作神器Spacescape
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/46444569 作者:car ...
- [Recompose] Add Local State to a Functional Stateless Component using Recompose
Learn how to use the 'withState' and 'withHandlers' higher order components to easily add local stat ...
- css强制不换行 多出的字省略号
width: 100%;//需要指定宽度 overflow: hidden;//溢出隐藏 text-overflow: ellipsis; white-space: nowrap;//强制不换行 te ...
- 新版itunes添加铃声
iTunes 铃声制作-图文教程 ① 点选设备iPhone - 勾选手动管理音乐和视频 - 点击应用 注意:因本操作涉及iPhone内音乐和视频,请操作前先对音乐和视频进行相关备份,以免同步后被刷掉造 ...
- [SCSS] Use Standard Built-in SCSS Functions for Common Operations
We can use javascript for color and opacity variations, math, list and map logic or to see if someth ...
- SQLite编码
•SQLite编码 •讲师:李明杰 •技术博客:http://www.cnblogs.com/mjios •SQLite3 •在iOS中使用SQLite3,首先要添加库文件libsqlite3.dyl ...
- 【a601】雇佣计划
Time Limit: 1 second Memory Limit: 32 MB [问题描述] 一位管理项目的经理想要确定每个月需要的工人,他知道每月所需的最少工人数.当他雇佣或解雇一个工人时,会有一 ...