Day5 - D - Conscription POJ - 3723
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, 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
Output
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 思路:最小生成树板子题,注意这一题是求最大权,取反就是最小生成树,且这一题不是所有点都是连通的,用prim麻烦,直接kruskal最小生成森林即可
const int maxm = ; struct edge {
int u, v, w;
edge(int _u=-, int _v=-, int _w=):u(_u), v(_v), w(_w){}
bool operator<(const edge &a) const {
return w < a.w;
}
} Edge[]; int fa[maxm], T, N, M, V, R; void init() {
for(int i = ; i <= V; ++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 main() {
scanf("%d", &T);
while(T--) {
int t1, t2, t3, u, v;
scanf("%d%d%d", &N, &M, &R);
V = N + M;
init();
int sum = ;
for(int i = ; i < R; ++i) {
scanf("%d%d%d", &t1, &t2, &t3);
Edge[i] = edge(t1, t2+N, -t3);
}
sort(Edge, Edge+R);
for(int i = ; i < R; ++i) {
u = Edge[i].u, v = Edge[i].v;
u = Find(u), v = Find(v);
if(u != v) {
sum += Edge[i].w;
Union(u,v);
}
}
printf("%d\n", V* + sum);
}
return ;
}
Day5 - D - Conscription POJ - 3723的更多相关文章
- Conscription(POJ 3723)
原题如下: Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16584 Accepted: 57 ...
- MST:Conscription(POJ 3723)
男女搭配,干活不累 题目大意:需要招募女兵和男兵,每一个人都的需要花费1W元的招募费用,但是如果有一些人之间有亲密的关系,那么就会减少一定的价钱,如果给出1~9999的人之间的亲密关系,现在要你求 ...
- poj - 3723 Conscription(最大权森林)
http://poj.org/problem?id=3723 windy需要挑选N各女孩,和M各男孩作为士兵,但是雇佣每个人都需要支付10000元的费用,如果男孩x和女孩y存在亲密度为d的关系,只要他 ...
- POJ 3723 Conscription(并查集建模)
[题目链接] http://poj.org/problem?id=3723 [题目大意] 招募名单上有n个男生和m个女生,招募价格均为10000, 但是某些男女之间存在好感,则招募的时候, 可以降低与 ...
- POJ 3723 Conscription MST
http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...
- POJ 3723 Conscription 最小生成树
题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...
- POJ 3723 Conscription
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6325 Accepted: 2184 Desc ...
- POJ 3723 Conscription (Kruskal并查集求最小生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14661 Accepted: 5102 Des ...
- 【POJ - 3723 】Conscription(最小生成树)
Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ...
随机推荐
- idna与utf-8编码漏洞
来自Black hat 2019 原理什么是IDN?国际化域名(Internationalized Domain Name,IDN)又名特殊字符域名,是指部分或完全使用特殊文字或字母组成的互联网域名, ...
- 什么是redis事务
一.什么是redis事务? 可以一次性执行多条命令,本质上是一组命令的集合.一个事务中的所有命令都会序列化,然后按顺序地串行化执行,而不会被插入其他命令 二.Redis 事务可以做什么? 一个队列中, ...
- 笔记-AJAX
笔记-AJAX 1. 简介 Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML AJAX 是一种用于创建快速动态网页的技术 ...
- python批量提取哔哩哔哩bilibili视频
# -*- coding: utf-8 -*- """ Created on Tue Jan 29 13:26:41 2019 @author: kwy "&q ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:使段落突出显示
<!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...
- Week of Code:GG
题意是给出一个数n,一个长度为n-1的字符串.求的是1到n符合要求的序列的数量,该序列需要满足当该位置为G时,这个位置的数大于后面位置的数.当该位置为L时,这个位置的数要小于后面位置的数.最后数量模m ...
- (转)js实现倒计时效果(年月日时分秒)
原文链接:http://mengqing.org/archives/js-countdown.html 之前做的活动页面很多都用到了倒计时功能,所以整理下下次直接用.(用的是张鑫旭写的一个倒计时,稍作 ...
- SRS源码——调用FFmpeg参数问题
在SRS的Ingest功能中,会调用本地FFmpeg进行拉流转码, 调用的核心代码在srs_app_ffmpeg.cpp 的 SrsFFMPEG::start() 中: // memory leak ...
- js图片瀑布流效果
要实现图片瀑布流效果,首先得准备几张图片. html的部分比较简单就是将图片加载到浏览器就可以了 代码如下(注意放的图片多一点要不然之后无法滑动鼠标就无法达到瀑布流效果): <!DOCTYPE ...
- Python 基础之函数初识与函数参数
一.函数初识 定义:满足某一个方法 满足某一个功能#(1)功能(包裹一部分代码 实现某一个功能 达成某一个目的)#(2)可以反复调用,提高代码的复用性,提高开发效率,便于维护管理#(3)函数的基本格式 ...