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 JNI编程(七)——使用AndroidStudio编写第一个JNI程序

    版权声明:本文出自阿钟的博客,转载请注明出处:http://blog.csdn.net/a_zhon/. 目录(?)[+] 1.简单介绍一下NDK和JNI NDK:NDK是Native Develop ...

  2. MySQL 监控-innotop

    innotop 编写者Balon Schwartz,<高性能MySQL>的作者之一. innotop的作用为实时地展示服务器正在发生的事情,监控innodb,监控多个MySQL实例,是一款 ...

  3. 安装innotop

    安装方法一: 下载地址:https://github.com/innotop/innotop yum install -y perl-TermReadKey yum install -y perl-D ...

  4. 编辑器vim简介

    vi简介 vi是"Visual interface"的简称,它在Linux上的地位就仿佛Edit程序在DOS上一样.它可以执行输出.删除.查找.替换.块操作等众多文本操作,而且用户 ...

  5. 【14.06%】【hdu 5904】LCIS

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  6. 【p081】ISBN号码

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定 ...

  7. QT学习记录之环境搭建

    作者:朱金灿 来源:http://blog.csdn.net/clever101 1. 安装qt-win-opensource-4.8.5-vs2008.exe(对应的IDE是VS2008),安装路径 ...

  8. CEO 系列之一:如何当好创业公司 CEO?(不要用战术的勤奋掩盖战略的懒惰,在创业过程中,最核心问题,就是能把创业情怀变成具体问题。这个问题越具体越好)

    1. 创业公司要先定一个目标,要善于把目标简化, 分解成一个, 一个更具体,更简单的问题2. 针对简单的问题进行聚焦, 做深做强3. 在做的过程中, 把断地推出自己的产品到市场上去试错, 要用事实来证 ...

  9. 简体和繁体加起来有六七万个汉字,所以Unicode只能排除一些几乎不用的汉字,Unicode编码的熟悉与研究过程(内附全部汉字编码列表)

    我有一个问题是:是不是会有个别汉字无法在Unicode下表示,这种情况下就不能完全显示了? 各种编码查询表:http://bm.kdd.cc/ ---------------------------- ...

  10. 学习游戏服务器开发必看,C++游戏服务器开发常用工具介绍

    C++游戏服务器开发常用工具介绍 在软件开发过程中需要使用的工具类型实属众多,从需求建模到软件测试,从代码编译到工程管理,这些工具都对项目有着不可替代的作用.庄子有云,"吾生也有涯,而知也无 ...