POJ3723--Conscription(MST)WRONG
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
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
using namespace std;
int father[];
int n,m,r;
struct node{
int u,v,cost;
}edge[];
int cmp(struct node a,struct node b){
return a.cost<b.cost;
}
int found(int x){
if(x!=father[x])
father[x]=found(father[x]);
return father[x];
}
void unite(int x,int y){
x=found(x);
y=found(y);
if(x==y)
return;
father[x]=y;
}
bool same(int x,int y){
return found(x)==found(y);
}
int kruskal(){
int i,ans;
struct node e;
ans=;
sort(edge,edge+r,cmp);
for(i=;i<r;i++){
e=edge[i];
if(!same(e.u,e.v)){
unite(e.u,e.v);
ans+=e.cost;
}
}
return ans;
} //kruskal模板
int main(){ //将每个人的费用取反,则变为求最小生成森林
int t,i;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&r);
for(i=;i<;i++)
father[i]=i;
for(i=;i<r;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].cost);
edge[i].v+=n; //并查集,将女兵的编号加N
edge[i].cost*=-;
}
printf("%d\n",*(n+m)+kruskal());
}
return ;
}
POJ3723--Conscription(MST)WRONG的更多相关文章
- POJ3723 Conscription 【并检查集合】
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8071 Accepted: 2810 Desc ...
- POJ3723 Conscription
http://poj.org/problem?id=3723 这题虽然简单,但是还是错了很多次. 因为这题构建的图可能是不连通的.也就是说可能有很多棵树. 所以我以前写的并查集用在这上面会出问题的. ...
- POJ 3723 Conscription MST
http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...
- MST:Conscription(POJ 3723)
男女搭配,干活不累 题目大意:需要招募女兵和男兵,每一个人都的需要花费1W元的招募费用,但是如果有一些人之间有亲密的关系,那么就会减少一定的价钱,如果给出1~9999的人之间的亲密关系,现在要你求 ...
- Conscription poj3723(最大生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6870 Accepted: 2361 Desc ...
- Conscription [POJ3723] [最小生成树]
Description: Windy有一个国家,他想建立一个军队来保护他的国家. 他召集了N个女孩和M男孩,想把他们雇佣成为他的士兵. 要无偿雇佣士兵,必须支付10000元. 女孩和男孩之间有一些关系 ...
- poj3723 MST好题 kruskal
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【POJ - 3723 】Conscription(最小生成树)
Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ...
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
随机推荐
- if 循环的深入理解 哈希表的一种应用
哈希表的值作为一个颜色容器,值默认为标识1, 表示未曾用过,若用过标识为0: 1: 程序第一步 遍历哈希表,查找标识为1 未曾用过的颜色 我用了这个: string colorno_us ...
- istio-jaeger-spring boot调用链配置
istio-jaeger-spring boot调用链配置 虽然,istio ingress controller已经生成了jaeger 记录所需要的信息,但是多个分布式之间没法清晰记录相互之间的依赖 ...
- PAT 1087 有多少不同的值(20)(STL—set)
1087 有多少不同的值(20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数 ...
- swoole的EventLoop学习
我们先使用php来写一个socket的服务端.先从最开始的模型开始将起逐步引申到为何要使用eventloop 1.最简单的socket服务端,直接按照官方文档来执行 <?php $sock = ...
- http协议(一)一些基础知识
当我们在浏览器的地址栏中输入网址,然后点击回车,接着,浏览器就会呈现出我们需要的web界面,那么,这个界面是怎么产生的? web的界面是根据我们输入的URL(网址.地址),浏览器从服务器端获取对应的文 ...
- BZOJ 1093 [ZJOI2007]最大半连通子图 - Tarjan 缩点
Description 定义一个半联通图为 : 对任意的两个点$u, v$,都有存在一条路径从$u$到$v$, 或从$v$到$u$. 给出一个有向图, 要求出节点最多的半联通子图, 并求出方案数. ...
- HDU 5988.Coding Contest 最小费用最大流
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- swift MD5 加密方法
引入OC类库 md5.h: #import <UIKit/UIKit.h> @interface Md5Controller : UIViewController @end md5.m: ...
- Java开发环境安装配置
电脑配置:Win7 64位 JDK1.8+Apache Tomcat8.5.4 下载JDK1.8 全名: Java SE Development Kit 8u151,下载最新稳定版本 下载地址:htt ...
- is not allowed to connect to this MySQL server解决办法
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; myuser:代表你 ...