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

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 <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef struct abcd
{
int x,y,z;
} abcd;
bool cmp(abcd x,abcd y)
{
return x.z>y.z;
}
abcd a[];
int p[];
int findset(int x)
{
int i,px=x;
while(px!=p[px])px=p[px];
while(x!=px)
{
i=p[x];
p[x]=px;
x=i;
}
return px;
}
int main()
{
int t,n,m,r,i,j,x,y,z;
cin>>t;
while(t--)
{
scanf("%d%d%d",&n,&m,&r);
for(i=; i<m+n; i++)p[i]=i;
for(i=; i<r; i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
a[i].y+=n;
}
sort(a,a+r,cmp);
long long sum=*(m+n);
for(i=; i<r; i++)
{
int x1=findset(a[i].x),y1=findset(a[i].y);
if(x1!=y1)
{
sum-=a[i].z;
p[x1]=y1;
}
}
cout<<sum<<endl;
}
}

Conscription poj3723(最大生成树)的更多相关文章

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

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

  2. 【图论】POJ-3723 最大生成树

    一.题目 Description Windy has a country, and he wants to build an army to protect his country. He has p ...

  3. POJ3723 Conscription 【并检查集合】

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

  4. POJ3723 Conscription

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

  5. POJ 3723 Conscription 最小生成树

    题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...

  6. 《挑战程序设计竞赛》2.5 最小生成树 POJ3723 3169 1258 2377 2395 AOJ2224(1)

    POJ3723 http://poj.org/problem?id=3723 题意 windy要组建一支军队,召集了N个女孩和M个男孩,每个人要付10000RMB,但是如果一个女孩和一个男孩有关系d的 ...

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

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

  8. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  9. NOIP 2013 货车运输 最大生成树加DFS巧妙AC

    #include<set> #include<map> #include<cmath> #include<queue> #include<stac ...

随机推荐

  1. 播放视频 IOS 与安卓的不同

    安卓:video IOS:canvas 具体原因请见上传文件“宝马视频安卓与IOS的区别”

  2. 浅述 Java 并发

    浅述 Java 并发 volatile volatile只能保证变量对各个线程的可见性,但不能保证原子性.关于 Java语言 volatile 的使用方法就不多说了,我的建议是 除了 配合packag ...

  3. java程序的内存分配

    java程序的内存分配 JAVA 文件编译执行与虚拟机(JVM)介绍 Java 虚拟机(JVM)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的 ...

  4. 第2阶段——编写uboot之硬件初始化和制作链接脚本lds(1)

    目标: 1.关看门狗 2.设置时钟 3.初始化SDRAM (初始化寄存器以及清除bss段) 4.重定位 (将nand/nor中代码COPY到链接地址上,需要初始化nandflash,读flash) 5 ...

  5. 转:【Java集合源码剖析】HashMap源码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/36034955   您好,我正在参加CSDN博文大赛,如果您喜欢我的文章,希望您能帮我投一票 ...

  6. 我来给.Net设计一款HttpClient

    1前言 时间飞快,转眼半年,碌碌无为,眼下就要三十而立,回想三年前的今天,我将NetworkSocket库开放到github,一直在更新与学习,不求有这个库能有多好,只求自己在过程能成长,将领悟到一些 ...

  7. java--利用exe4j生成.exe的可执行文件

    工具:eclipse,exe4j,jre,这三个都可以直接在官方网站下载,下面所用到的都是最新版的. 前期准备:用eclipse编好需要生成.exe文件的project,另外exe4j需要一个注册码, ...

  8. 201521123070 《JAVA程序设计》第8周学习总结

    1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.泛型简介:泛型程序设计,编写的代码可被不同类型的对象所重用,Java中一个集合可以放任何类 型的对象,因为 ...

  9. 201521123009 《Java程序设计》第6周学习总结

    1. 本周学习总结 2. 书面作业 Q1:clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 用protected修 ...

  10. 201521123077 《Java程序设计》第4周学习总结

    1. 本周学习总结 几种简单说明注释的使用 抽象类与抽象方法 super调用父类的方法 2. 书面作业 Q1.注释的应用使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看. ...