【图论】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 ...
随机推荐
- HBase--大数据系统的数据库方案
本文主要围绕以下三方面来讨论HBase:是什么.为什么.怎样做. 1. 什么是HBase HBase是一个开源的.分布式的.非关系型数据库,其设计思想来源于Google的Big Table.通过集群管 ...
- lua通用数据类型
TValue结构 TValue这个结构体是Lua的通用结构体,,Lua中的所有的数据都可以使用这个结构体来表示.很容易想到,在面向对象中,这个结构体是一个基类,派生出来的都是其他的子类. TValue ...
- 使用Random随机生成[min,max]之间的整数:
如下代码是生成范围在min到max之间的随机整数(包括min和max): import java.util.Random; /** * 生成[min,max]的随机整数 * @author mei ...
- 4 伪ajax:jsonp、cors 跨域请求
一.同源策略 https://www.cnblogs.com/yuanchenqi/articles/7638956.html 同源策略(Same origin policy)是一种约定,它是浏览器最 ...
- 前端- html -总结
html概述 head标签 title 显示网站的标题 meta 提供有关页面的原信息 link 链接css资源文件.网站图标 style 定义内部样式表 script 链接脚本js文件 body标签 ...
- Codeforces 469 D. Two Sets (并查集)
题目链接:Two Sets 题意: 有n个数,要分成A.B两组,要求如果x∈A则a-x∈A,如果x∈B则b-x∈B,问是否存在一种符合要求的分法. 题解: 并查集,先增加两个点表示A和B集合的根,对于 ...
- 手把手带你打造一个 Android 热修复框架(上篇)
本文来自网易云社区 作者:王晨彦 前言 热修复和插件化是目前 Android 领域很火热的两门技术,也是 Android 开发工程师必备的技能. 目前比较流行的热修复方案有微信的 Tinker,手淘的 ...
- Bitcoin区块链攻击方式
目录 重放攻击-- 非人为攻击 其他攻击 重放攻击-- 非人为攻击 重放攻击 Replay Attach 攻击者重复发送相同的数据库包到目的主机,用以欺骗系统 用支付宝付款信息重复项商家索取商品 比特 ...
- 通过ftp同步服务器文件:遍历文件夹所有文件(含子文件夹、进度条);简单http同步服务器文件实例
该代码主要实现,指定ftp服务地址,遍历下载该地址下所有文件(含子文件夹下文件),并提供进度条显示:另外附带有通过http地址方式获取服务器文件的简单实例 废话不多说,直接上代码: 1.FTPHelp ...
- ubuntu/linux中安装Tomcat(附图解详细步骤)
我的linux系统使用的是ubuntu14 1.首先需要先到Tomcat官网上下载对应linux系统的压缩包,可以直接在Ubuntu系统中进行下载,下载后的默认路径为主文件夹路径下的下载文件目录下 注 ...