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. Ubuntu下成功安装QQ2013

    阳光小强最近用Win8系统感觉特别不爽,中午果断换了一个win7系统,又想着能不能搞个双系统(最近在看一些linux东西),于是就开始整起来.结果并不好,linux虽然整好了,但是硬盘全部格式化了,当 ...

  2. angular项目中各个文件的作用

    原文地址 https://www.jianshu.com/p/176ea79a7101 大纲 1.对angular项目中的一些文件的概述 2.对其中一些文件的详细描述 2.1.package.json ...

  3. OpenStack 之 Nova Compute 的代码结构图

    nova-compute 的代码结构图 如上图所看到的, 类图中最重要的三个Category Manager: 核心的业务类.提供实际的业务操作.比如启动虚拟机等等. Service: 每一个serv ...

  4. Lucene学习总结之五:Lucene段合并(merge)过程分析 2014-06-25 14:20 537人阅读 评论(0) 收藏

    一.段合并过程总论 IndexWriter中与段合并有关的成员变量有: HashSet<SegmentInfo> mergingSegments = new HashSet<Segm ...

  5. 并发队列ConcurrentLinkedQueue 和 阻塞队列LinkedBlockingQueue用法

    在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出).Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是BlockingQ ...

  6. Spring-boot更改成war包的方式

    转载至:  https://blog.csdn.net/zhuwei_clark/article/details/82114102  Step1 修改启动类 Step2 修改配置文件为properti ...

  7. [转载]Ocelot简易教程(二)之快速开始1

    Ocelot是为.net core量身定做的,目前是基于 netstandard2.0进行构建的. .NET Core 2.1中如何使用呢? 安装NuGet package 使用nuget安装Ocel ...

  8. 解决Eclipse 启动后总是Building WorkSpace(sleeping)

    打开eclipse后eclipse总是在Building WorkSpace(sleeping),我的解决方案是,Project -> Building AutoMatically关闭就好了,以 ...

  9. [TypeScript] Using ES6 and ESNext with TypeScript

    TypeScript is very particular about what is and isn't allowed in a TS file to protect you from commo ...

  10. [Flow] Declare types for application

    In Flow, you can make global declarion about types. Run: flow init It will generate .flowconfig file ...