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, NM and R.
Then R lines followed, each contains three integers xiyi and di.
There is a blank line before each test case.

1 ≤ NM ≤ 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 <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的更多相关文章

  1. POJ3723 Conscription 【并检查集合】

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8071   Accepted: 2810 Desc ...

  2. POJ3723 Conscription

    http://poj.org/problem?id=3723 这题虽然简单,但是还是错了很多次. 因为这题构建的图可能是不连通的.也就是说可能有很多棵树. 所以我以前写的并查集用在这上面会出问题的. ...

  3. POJ 3723 Conscription MST

    http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...

  4. MST:Conscription(POJ 3723)

      男女搭配,干活不累 题目大意:需要招募女兵和男兵,每一个人都的需要花费1W元的招募费用,但是如果有一些人之间有亲密的关系,那么就会减少一定的价钱,如果给出1~9999的人之间的亲密关系,现在要你求 ...

  5. Conscription poj3723(最大生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6870   Accepted: 2361 Desc ...

  6. Conscription [POJ3723] [最小生成树]

    Description: Windy有一个国家,他想建立一个军队来保护他的国家. 他召集了N个女孩和M男孩,想把他们雇佣成为他的士兵. 要无偿雇佣士兵,必须支付10000元. 女孩和男孩之间有一些关系 ...

  7. poj3723 MST好题 kruskal

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  8. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  9. 【POJ - 3723 】Conscription(最小生成树)

    Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ...

  10. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

随机推荐

  1. (转)Android中Parcelable接口用法

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  2. 立即响应ScrollView上的子视图的手势

    self.myScrollView.delaysContentTouches = YES; self.myScrollView.CanCancelContentTouches=NO; 写了一个继承sc ...

  3. Unknown type name 'NSString' 解决方案

    今天看到个问题,编辑工程提示Unknown type name 'NSString',如下图 导致出现异常的原因是是因为工程中添加了ZipArchive(第三方开源解压缩库) 一般情况下出现“Unkn ...

  4. Linux 终端设备

    <Linux终端设备详解> https://www.cnblogs.com/shineshqw/articles/2423989.html

  5. Python3字符编码

    编码 字符串是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节 ...

  6. Servlet封装类

    Servlet 提供了四个封装类: public class ServletRequestWrapper extends java.lang.Object implements ServletRequ ...

  7. Python之路(第二十七篇) 面向对象进阶:内置方法、描述符

    一.__call__ 对象后面加括号,触发执行类下面的__call__方法. 创建对象时,对象 = 类名() :而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()( ...

  8. ajax 跨域请求没有带上cookie 解决办法

    公司项目前后端分离.. 前端全部html 静态页面.. 后端java 接口服务 由于前后端分离,出现跨域问题. 为了解决,我们使用jsonp 方式请求接口服务,暂时解决了跨域问题(使用jquery a ...

  9. python r r+ w w+ rb 文件打开模式的区别

    # 只读模式with open ( "file.txt" ,'r' ) as f:        for line in f.readlines():                ...

  10. AOP (切点表达式讲解)

    Spring EL表达式:: 1.execution 表达式 语法格式: execution(返回类型.包名.类名.方法名(参数表)) exection(*.com.xxx.AService.*(.. ...