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, NM and R.

Then R lines followed, each contains three integers xiyi and di.

There is a blank line before each test case.

1 ≤ NM ≤ 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

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 【并检查集合】的更多相关文章

  1. HDU 1272 小希迷宫(并检查集合)

    意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...

  2. hdu1325 Is It A Tree?并检查集合

    pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...

  3. URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)

    意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...

  4. CodeForces 277A Learning Languages (并检查集合)

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...

  5. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. zoj 3659 并检查集合

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...

  7. uva 11987 Almost Union-Find (并检查集合)

    标题效果: 三操作. 1. 合并两个集合 2.代替所述第二组的第一个元素 3.输出设置数量,并.. IDEAS: 使用p该元素的记录数,其中集合,建立并查集. #include <cstdio& ...

  8. 《算法导论》2.3-7 检查集合中是否存在两数字和为指定的X--算法和证明

    习题2.3-7:设计一个算法,对于一个给定的包含n个整数的集合S和另一个给定的整数X,该算法可以在时间内确定S中是否存在两个元素,使得它们的和恰为X. 解题思路:首先应该想到的是先用一个的排序算法对S ...

  9. POJ3723 Conscription

    http://poj.org/problem?id=3723 这题虽然简单,但是还是错了很多次. 因为这题构建的图可能是不连通的.也就是说可能有很多棵树. 所以我以前写的并查集用在这上面会出问题的. ...

随机推荐

  1. Android系统开发(1)——GCC编译器的编译和安装过程

    GCC编译器介绍 GCC编译器(GNG C Compiler)是GNU项目中符合ANSI C标准的编译系统,能够编译C  C++  Object C等语言编写的程序,同时GCC也是一个交叉编译器,特别 ...

  2. 如何在移动web模仿客户端给input输入框添加自定义清除按钮

    项目有个需求就是在input输入框添加清除按钮,网上查找资料加上自己琢磨终于弄出来了. 灵感来自于 http://www.zhangxinxu.com/wordpress/?p=4077 由于项目已经 ...

  3. 魔兽争霸war3心得体会(二):狗转蜘蛛,DK光环+游侠二发

    最近几周,勤奋地在QQ对战平台上,练习war3对战. 10年玩到14年初, 也玩了很多,主要是抱着"随便玩玩"的心态,结果也很显然,可以轻松打赢中等电脑,以及AI不够高的发狂的Hu ...

  4. 在Scope利用Content sharing Widget来分享内容

    在最新的Scope Widget中,有一个新的Content Sharing Widget.我们能够利用这个Widget来分享我们的图片到信息.Facebook,Twitter等渠道.比方,在我们的S ...

  5. [GraphQL] Deploy a GraphQL dev playground with graphql-up

    In this lesson we'll use a simple GraphQL IDL schema to deploy and explore a fully functional GraphQ ...

  6. 【37.21%】【codeforces 721B】Passwords

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. [SCSS] Reuse Styles with the SCSS @mixin Directive

    Copy/pasting the same code is redundant and updating copy/pasted code slows development velocity. Mi ...

  8. Django表单上传

    任务描述:实现表单提交(上传文件) 1.项目目录: 2.源代码: regist.html <!DOCTYPE html> <html lang="en"> ...

  9. php输出文件,数组

    file_put_contents('C://zll.txt',var_export($data,true));//输出数组 file_put_contents('C://zll.txt','你好啊' ...

  10. android WebView总 结

    浏览器控件是每个开发环境都具备的,这为马甲神功提供了用武之地,windows的有webbrowser,android和ios都有webview.只是其引擎不同,相对于微软的webbrowser,and ...