【图论】POJ-3723 最大生成树
一、题目
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, 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
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 最大生成树的更多相关文章
- POJ 3723 Conscription MST
http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...
- 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 最小生成树
题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...
- POJ 3723
最大生成树 #include<iostream> #include<cstdio> #include<cstring> #include<set> #i ...
- POJ 3723 Tree(树链剖分)
POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...
- MST:Conscription(POJ 3723)
男女搭配,干活不累 题目大意:需要招募女兵和男兵,每一个人都的需要花费1W元的招募费用,但是如果有一些人之间有亲密的关系,那么就会减少一定的价钱,如果给出1~9999的人之间的亲密关系,现在要你求 ...
- Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...
- 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...
- POJ 3723 Conscription
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6325 Accepted: 2184 Desc ...
随机推荐
- Playing audio from Node.js using Edge.js
http://tomasz.janczuk.org/2014/06/playing-audio-from-nodejs-using-edgejs.html
- Android签名验证漏洞POC及验证
poc实际上就是一段漏洞利用代码,以下是最近炒得很火Android签名验证漏洞POC,来自https://gist.github.com/poliva/36b0795ab79ad6f14fd8 #!/ ...
- P3702 [SDOI2017]序列计数
P3702 [SDOI2017]序列计数 链接 分析: 首先可以容斥掉,用总的减去一个质数也没有的. 然后可以dp了,f[i][j]表示到第i个数,和在模p下是j的方案数,矩阵快速幂即可. 另一种方法 ...
- Oracle Database Link 连接数据库复制数据
--1. 创建dblink连接 create database link mdm66 connect to lc019999 identified by aaaaaa using '10.24.12. ...
- 洛咕 P3321 [SDOI2015]序列统计
显然dp就是设\(f[i][j]\)表示dp了i轮,对m取膜是j的方案数 \(f[i][xy\mod m]=f[i-1][x]\times f[i-1][y]\) 这是\(O(nm^2)\)的 像我这 ...
- 给浏览器和各种软件配置 http https socks5 代理 proxy
只需要像在 idea 里一样,配置好sr的本地代理ip:127.0.0.1 本地代理端口:1080 浏览器和软件的流量即可走 sr ,就能被 sr 代理了 用上代理之后,确实快了好多,这里是在打开 ...
- 【Java源码解析】Thread
简介 线程本质上也是进程.线程机制提供了在同一程序内共享内存地址空间运行的一组线程.对于内核来讲,它就是进程,只是该进程和其他一下进程共享某些资源,比如地址空间.在Java语言里,Thread类封装了 ...
- (webapp)微信和safri 对于html5 部分功能不兼容,多选或单选下拉框去除边框无效果。
1 appearance:none; 2 -moz-appearance:none; /* Firefox */ 3 -webkit-appearance:none; /* Safari 和 Chro ...
- SpringBoot日记——Thymeleaf进阶小篇
我们做好了登录页,那设计一个场景吧,比如我们登录后跳转到公司主页,想从公司主页再跳转到员工列表页: 这样的场景我们该如何实现,首先要知道一些基础知识,就是SpringBoot的一些关于请求的架构知识: ...
- django套用模板404报错小结
首先,我的项目名是MyProject.每次当我运行,然后测试页面的时候,总是弹出 其实根据stackoverflow上某大佬的解释大意就是在setting.py和urls.py的匹配上出了问题 此处放 ...