题目链接:

题目

Conscription

Time Limit: 1000MS

Memory Limit: 65536K

问题描述

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.

输入

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

输出

For each test case output the answer in a single line.

样例

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

output

71071

54223

题意

现在选N个男生和M个女生进入部队,如果男生u和女生v有关系,那么如果有一个已经在部队里面了,那另一个的费用只需10000-p(关系系数)。并且每个人进入部队时他只能使用和最多一个人的关系。

问最少的花费招到所有的人。

题解

如果使用的关系之间出现了环,那么就不必有至少一个人同时使用了两个关系,所以题目就转化成了求最大生成树了。

代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 21111; struct Edge {
int u, v, w;
Edge(int u, int v, int w) :u(u), v(v), w(w) {}
bool operator < (const Edge& tmp) const {
return w > tmp.w;
}
}; int n, m, r;
int fa[maxn];
vector<Edge> egs; int find(int x) { return fa[x] = fa[x] == x ? x : find(fa[x]); } void init() {
for (int i = 0; i <= n + m; i++) fa[i] = i;
egs.clear();
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d%d", &n, &m,&r);
init();
while (r--) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
egs.push_back(Edge(u,v+n,w));
}
sort(egs.begin(), egs.end());
int cnt = 0;
for (int i = 0; i < egs.size(); i++) {
Edge& e = egs[i];
int pu = find(e.u);
int pv = find(e.v);
if (pu != pv) {
cnt += e.w;
fa[pv] = pu;
}
}
printf("%d\n", (n + m) * 10000 - cnt);
}
return 0;
}

POJ 3723 Conscription 最小生成树的更多相关文章

  1. POJ 3723 Conscription (Kruskal并查集求最小生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Des ...

  2. POJ 3723 Conscription【最小生成树】

    题意: 征用一些男生和女生,每个应都要给10000元,但是如果某个男生和女生之间有关系,则给的钱数为10000减去相应的亲密度,征集一个士兵时一次关系只能使用一次. 分析: kruskal求最小生成树 ...

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

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

  4. POJ 3723 Conscription

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

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

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

  6. POJ 3723 Conscription MST

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

  7. 【POJ - 3723 】Conscription(最小生成树)

    Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ...

  8. POJ 3723 征兵问题(最小生成树算法的应用)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15923   Accepted: 5510 Des ...

  9. Conscription(POJ 3723)

    原题如下: Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16584   Accepted: 57 ...

随机推荐

  1. android sdk manager 无法更新

    1.在C:\Windows\System32\drivers\etc找到Hosts文件用记事本打开,在最末尾添加如下代码,保存关闭: #Google主页203.208.46.146 www.googl ...

  2. WScript.SendKeys()的sendkeys发送组合键以及特殊字符

    SendKeys.Send("^+{TAB}"); 使用SendKeys将键击和组合键击发送到活动应用程序.此类无法实例化.若要发送一个键击给某个类并立即继续程序流,请使用Send ...

  3. UI5_HomeWorkCompanyViewController

    // // ItemCompany.h // UI5_HomeWork // // Created by zhangxueming on 15/7/3. // Copyright (c) 2015年 ...

  4. (转)Linux vmstat命令实战详解

    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...

  5. (转)hessian源码分析(一)------架构

    在计费中心的对外交互这块采用了hessian,有必要对hessian的运行机理和源码做一定的解析. 大致翻了翻源码后,发现hessian的主要结构分客户端与服务端,中间基于http传输.客户端主要做的 ...

  6. 胸腺嘧啶“T”

    4.下列物质或结构中含胸腺嘧啶“T”的是( )A.DNA聚合酶 B.烟草花叶病毒C.ATP D.B淋巴细胞中的线粒体

  7. oracle连接和执行流程总结

    参考关于oracle连接及一个事务的完整流程分析的资料,做整理如下 参考资料: http://blog.csdn.net/wyzxg/archive/2010/08/16/5815335.aspx h ...

  8. 《Linux系统静态路由和火墙路由》

    本篇主要写的是关于静态路由表的添加,和如何让你不能上网的主机通过火墙路由表实现上网的功能. 静态路由表: 要是你的主机是2块网卡,并且做了网卡的绑定,依照我下面的方法是成功不了的,你可以去编辑: # ...

  9. Linux--变量与虚拟内存

    定义一个变量:存储类型  数据类型  变量名 存储类型(变量存储的位置):auto.register.static.extern 1.auto:对于局部变量,atuo可以缺省.位置:栈 2.exter ...

  10. javascript面向对象--自定义类型

    Javascript是基于原型实现面向对象的,因此并没有类和接口,它的对象也与其他基于类的语言中的对象有所不同.在Javascript中,每个对象都是基于一个引用类型创建的,这个引用类型可以是原生类型 ...