一、题目

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, 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

二、思路&心得

三、代码

#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 最大生成树的更多相关文章

  1. POJ 3723 Conscription MST

    http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...

  2. poj - 3723 Conscription(最大权森林)

    http://poj.org/problem?id=3723 windy需要挑选N各女孩,和M各男孩作为士兵,但是雇佣每个人都需要支付10000元的费用,如果男孩x和女孩y存在亲密度为d的关系,只要他 ...

  3. POJ 3723 Conscription(并查集建模)

    [题目链接] http://poj.org/problem?id=3723 [题目大意] 招募名单上有n个男生和m个女生,招募价格均为10000, 但是某些男女之间存在好感,则招募的时候, 可以降低与 ...

  4. POJ 3723 Conscription 最小生成树

    题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...

  5. POJ 3723

    最大生成树 #include<iostream> #include<cstdio> #include<cstring> #include<set> #i ...

  6. POJ 3723 Tree(树链剖分)

    POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...

  7. MST:Conscription(POJ 3723)

      男女搭配,干活不累 题目大意:需要招募女兵和男兵,每一个人都的需要花费1W元的招募费用,但是如果有一些人之间有亲密的关系,那么就会减少一定的价钱,如果给出1~9999的人之间的亲密关系,现在要你求 ...

  8. Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏

    Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...

  9. 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏

    Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...

  10. POJ 3723 Conscription

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6325   Accepted: 2184 Desc ...

随机推荐

  1. JavaWeb基础—JDBC(二)事务与批处理

    一.批处理 这里给出PrepareStatement的示例,优点是可以发送预编译的SQL,缺点是SQL语句无法更换,但参数可以更换 批处理:多条语句的处理 mysql默认是关闭的,要打开需要在url后 ...

  2. JavaWeb基础—会话管理之Cookie

    一.什么是会话 打开浏览器,浏览各种资源,点击各种超链接,直至关闭浏览器,整个过程称为会话 二.会话管理的两种技术 1.Cookie 基于客户端.在客户端记录信息来确认用户身份.以cookie的形式写 ...

  3. WPF 学习笔记-设置属性使窗口不可改变大小

    原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...

  4. [COGS257]动态排名系统 树状数组套主席树

    257. 动态排名系统 时间限制:5 s   内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...

  5. iOS开发-通过正则表达式进行各种判断银行卡,车牌号,邮箱地址,QQ,身份证,全字母,仅输入字母或数字同时包含大小写字母和数字,仅能输入中文等

    /* *  验证银行卡号是否正确 *  车牌号验证 *  检验邮箱地址是否正确 *  手机号中间四位密文显示 *  判断QQ号是否正确(5-11位) *  判断身份证号是否正确(如末位为字母请用“x” ...

  6. P4198 楼房重建

    P4198 楼房重建 集中写博客= = 首先把高度变成斜率 然后就比较玄学了,首先用线段树维护一个区间的斜率最大值,和只看这个区间时能看见的楼房个数ans 然后更新时先更新max,再处理神奇的ans ...

  7. [BZOJ4028][HAOI2015]公约数数列[分块+分析暴力]

    题意 题目链接 分析 首先明确 \(xor\) 运算和 \(\rm gcd\) 没有联系! 注意到一个数字取 \(\rm gcd\) 且保证每次取 \(\rm gcd\) 值都会变小的话,最多取 \( ...

  8. tcp三次握手四次挥手那些事

    建立TCP需要三次握手才能建立,而断开连接则需要四次挥手.三次握手,四次挥手流程图如下: 一.首先看下如何通过三次挥手----------建立连接 首先客户端发送连接请求报文,服务端接受连接后回复AC ...

  9. 菜鸟vimer成长记——第4.2章、编程插件

    简介 这部分的插件是与编程相关的插件.主要涉及两大块:所有编程语言通用的插件,以及各个语言独有的插件.插件的数量和合理性,这可能是一个不断累积和修正的过程. 个人感觉,现在比较适合sh,html,ma ...

  10. 用 Python 带你看《我不是药神》

    我们都是小人物,我们都得了同一种病,我们都穷.——<我不是药神> 我不是程序员 我就是想求求你们,别动不动就拿篇10W+的文章来吓唬人好吗?说点有用的东西好吗?我们需要精神粮食不需要腐蚀精 ...