Conscription poj3723(最大生成树)
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, 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 <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(最大生成树)的更多相关文章
- Conscription [POJ3723] [最小生成树]
Description: Windy有一个国家,他想建立一个军队来保护他的国家. 他召集了N个女孩和M男孩,想把他们雇佣成为他的士兵. 要无偿雇佣士兵,必须支付10000元. 女孩和男孩之间有一些关系 ...
- 【图论】POJ-3723 最大生成树
一.题目 Description Windy has a country, and he wants to build an army to protect his country. He has p ...
- 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 最小生成树
题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...
- 《挑战程序设计竞赛》2.5 最小生成树 POJ3723 3169 1258 2377 2395 AOJ2224(1)
POJ3723 http://poj.org/problem?id=3723 题意 windy要组建一支军队,召集了N个女孩和M个男孩,每个人要付10000RMB,但是如果一个女孩和一个男孩有关系d的 ...
- 【POJ - 3723 】Conscription(最小生成树)
Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- NOIP 2013 货车运输 最大生成树加DFS巧妙AC
#include<set> #include<map> #include<cmath> #include<queue> #include<stac ...
随机推荐
- Statement和PrepareStatement区别
网上很多都说区别是PrepareStatement可以批处理.实际上二者都是可以进行批处理的. 区别在于: 1.PrepareStatement要求预编译的sql必须是格式固定,使用占位符获取参数. ...
- 极化码的matlab仿真(1)——参数设置
根据老师的安排,对于极化码的了解从仿真开始. 仿真的手段有很多种.可以利用C,C++,matlab等进行仿真的实现.其中matlab由于具有强大的函数库,和壮观的矩阵运算能力,被(我们老师课题组)看中 ...
- 使用反射+策略模式代替项目中大量的switch case判断
我这里的业务场景是根据消息类型将离线消息存入mongoDB不同的collection中.其中就涉及到大量的分支判断,为了增强代码的可读性和可维护性,对之前的代码进行了重构. 先对比一下使用反射+策略模 ...
- Java虚拟机JVM内存分区及代码执行机制
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt230 1. JVM体系结构 图1 JVM体系结构 方法区:存放JVM ...
- JDBC(一)之细说JDBC
Properties info = new Properties();//要参考数据库文档 info.setProperty("user", "root"); ...
- 201521123024《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 注意conve ...
- 201521123035《Java程序设计》第三周学习总结
本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 书面作业 1.代码 ...
- 201521123087 《Java程序设计》第2周学习总结
1.本周学习总结 类名第一个字母大写,类名下的方法如main第一个字母要小写: Java有三种基本数据类型:整型(byte,short,int,long,char),浮点型(float,double) ...
- 201521123100 《Java程序设计》 第1周学习总结
1. 本章学习总结 1.简单了解学习了Java及其开发环境,学习使用了Notepad++&eclipse开发软件的使用 2.熟练完成了使用Notepad++运行第一个Java程序"H ...
- 201521123037 《Java程序设计》第1周学习总结
#1.本章学习总结 了解java的发展历程 能够区分JVM.JRE.JDK之间的关系 了解文件名.类名要相同,且文件名大小写代表不同的名称 初步了解java编程语句 学会用控制台编译java程序 #2 ...