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 ...
随机推荐
- 解决Android Studio中Refreshing gradle project时间过长的最简单方法
Refreshing gradle project往往出现在Gradle出现更新,需要从墙外的网重新拖下来的前提下.具体可以查看以下文件:/path/to/project/gradle/wrapper ...
- NHibernate教程(18)--对象状态
本节内容 引入 对象状态 对象状态转换 结语 引入 在程序运行过程中使用对象的方式对数据库进行操作,这必然会产生一系列的持久化类的实例对象.这些对象可能是刚刚创建并准备存储的,也可能是从数据库中查询的 ...
- PHP面向对象知识点
public private protected __construct() __destruct() __set() __get() extends(继承) :: (重载) abstract cla ...
- 【2017集美大学1412软工实践_助教博客】团队作业8——第二次项目冲刺(Beta阶段)
题目 团队作业8: http://www.cnblogs.com/happyzm/p/6856179.html 团队作业8-1 beta冲刺计划 团队 新加入的成员,担当的角色,技术特点 下一阶段需要 ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.4.23
1.当天站立式会议照片 本次会议为第一次会议 本次会议在5号公寓1楼召开,本次会议内容: ①:做第一天的简单分工 ②:讨论每个人是否对安排的任务有苦难 ③:规定完成时间是在第二天之前 ④:遇到困难,及 ...
- 学号:201521123116 《java程序设计》第四周学习总结
1. 本周学习总结 2. 书面作业 Q1. 注释的应用使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图)答:注释的插入:注释以/开始,以/结束类注释/**.... ...
- Java实现猜字母游戏
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAE9CAYAAAB6Cu4FAAAgAElEQVR4nOy995OUR77u2f/H3tjdey ...
- 201521123064 《Java程序设计》第11周学习总结
1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. ① 定义Thread类的子类,覆盖Thread类的run()方法,然后创建该子类的实例(一般不用该方法,开销大. ...
- 201521123022 《Java程序设计》 第九周学习总结
1.本章学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 Q1.1 截图你的提交结果(出现学号) Q1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何 ...
- Ring3层 UNICODE_STRING
今天写驱动用到UNICODE_STRING,就在Ring3层抠了一些源代码,学习一下,不多说了上代码了 #pragma once #include <windows.h> #include ...