POJ 3723 Conscription (Kruskal并查集求最小生成树)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 14661 | Accepted: 5102 |
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.
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
题意:有N+M个顶点,边:两个顶点之间的亲密度。
单独获取某个顶点的花费是10000;
如果两个顶点的亲密度为x,我已经获取了其中一个顶点,那么获取另一个顶点花费10000-x。
现在你要把所有的顶点全部获取,算出最小花费。
【方法1】
我们可以想到这是一个森林,我们需要求最小生成树(最小花费)。
由于权值x越大花费越小,所以我们把权值取反为-x,求出最小生成树(通过关系获取比直接获取可以少花费多少),
然后再加上每个点花费的10000,就得到了我们的总花费。
【方法2】
你可能会说,直接把权值设置为10000-x也可以写,
但是考虑到我们求出最小生成树之后还要再求出连通分量,
总花费 = 最小生成树(通过关系获取)+ 连通分量*10000。 明显比前一种算法复杂度要高(主要是我还不会写连通分量)。 不过不会写不要紧,我们可以用Kruskal算法用到的并查集,
并查集中的parent数组保存的就是属于哪棵树,
所以算一下parent中有几个不同的值就可以得到连通分量。
Kruskal算法讲解:http://www.cnblogs.com/zhangjiuding/p/7796526.html
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
#define MAX_N 20010
#define MAX_E 50010
#define INF 2147483647 struct edge {
int u,v,cost;
}; bool cmp(const edge & e1,const edge &e2){
return e1.cost < e2.cost;
} edge es[MAX_E]; //边集
int V,E; //顶点数和边数 int par[MAX_N]; //par[i]表示i节点的父节点
int rank[MAX_N]; // 树的高度 //初始化n个元素
void init(int n){
for(int i = ;i < n; i++){
par[i] = i;
rank[i] = ;
}
} //查询包含x节点的树的根
int find(int x){
if(par[x] == x) return x;
else return par[x] = find(par[x]);
} //合并 x和y所属的集合
void unite(int x,int y){
x = find(x);
y = find(y);
if(x == y) return; if(rank[x] < rank[y]){
par[x] = y;
}else{
par[y] = x;
if(rank[x] == rank[y]) rank[x]++;
}
} //判断x和y是否属于同一个集合
bool same(int x,int y){
return find(x) == find(y);
} int kruskal(){
sort(es, es + E, cmp);
init(V);
int res = ;
for(int i = ;i < E; i++ ){
edge e = es[i];
if(!same(e.u,e.v)){
unite(e.u, e.v);
res += e.cost;
}
}
return res;
} int main(){
int t;
cin >> t;
while(t--){ int n,m,r;
cin >> n >> m >> r;
V = n+m;
E = r;
for(int i = ;i < r; i++){
edge e;
scanf("%d%d%d",&e.u,&e.v,&e.cost);
e.cost = - e.cost;
e.v += n;
es[i] = e;
} cout << V* + kruskal() << endl;
}
return ;
}
POJ 3723 Conscription (Kruskal并查集求最小生成树)的更多相关文章
- POJ 3723 Conscription(并查集建模)
[题目链接] http://poj.org/problem?id=3723 [题目大意] 招募名单上有n个男生和m个女生,招募价格均为10000, 但是某些男女之间存在好感,则招募的时候, 可以降低与 ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- Minimum Spanning Tree.prim/kruskal(并查集)
开始了最小生成树,以简单应用为例hoj1323,1232(求连通分支数,直接并查集即可) prim(n*n) 一般用于稠密图,而Kruskal(m*log(m))用于系稀疏图 #include< ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- TOJ 2815 Connect them (kruskal+并查集)
描述 You have n computers numbered from 1 to n and you want to connect them to make a small local area ...
- Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)
题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0 ...
- POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Description There are N v ...
随机推荐
- [实例]ROS使用OpenCV读取图像并发布图像消息在rviz中显示
思路: (1)使用opencv读取本地图像 (2)调用cv_bridge::CvImage().toImageMsg()将本地图像发送给rviz显示 一.使用opencv读取本地图像并发布图像消息 ( ...
- Sumblime Text3中使用vue-cli创建vue项目,代码不高亮,解决
问题如下:在Sumblime Text3中打开vue-cli常见的项目,代码一片灰色 解决如下: 第一步:下载文件Vue components 链接 GitHub - vuejs/vue-synta ...
- Activity-任务栈和启动模式
为什么需要了解关于Activity的任务栈,其实最直接的体现就是提高用户交互友好性. 举个例子,当我们去浏览一个新闻客户端的时候,我们进入了新闻详情页,在这个页面有相隔两条的新闻标题,当我们去点击这个 ...
- HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】
算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输 ...
- ZBrush中设置背面遮罩的两种方法
背面遮罩是ZBrush软件实时遮罩的一种,它的出现能够解决我们在模型雕刻时的一些问题.我们在 ZBrush®中雕刻一个比较薄的物体时,经常会不经意的雕刻到背面的物体.那么遇到此类状况该如何设置ZBru ...
- jq——css类
1 addClass(classname) 添加类 <script type="text/javascript"> $("input").clic ...
- Day 02 - 01 计算机的操作系统
操作系统 场景:有以下三个人 1.庄园主 -->相当于人类 2.管家 --> 把人类要传达的信息分配给奴隶,相当于操作系统 3.奴隶 -->为人类干活的机器 1.操作系统做了什么? ...
- Django配置MariaDB数据库
Django中配置MariaDB数据库.配置文件如下: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': ...
- UNIX系统高级编程——第四章-文件和目录-总结
文件系统: 以UNIX系统V文件系统为例: 磁盘分为区,每个分区都有自己的文件系统: i节点是固定长度的记录项,包含了文件的相关信息.目录项包含文件名和i节点号.stat结构中除文件名和i节点编号 ...
- 树形dp复习 树上依赖背包问题
选课 今天又看了一下这道题,竟然AC不了了 自己的学习效率有点低下 要明白本质,搞透彻 #include<bits/stdc++.h> #define REP(i, a, b) for(r ...