poj - 3723 Conscription(最大权森林)
http://poj.org/problem?id=3723
windy需要挑选N各女孩,和M各男孩作为士兵,但是雇佣每个人都需要支付10000元的费用,如果男孩x和女孩y存在亲密度为d的关系,只要他们其中有一个已经被选中,那么在选另一个人需要的费用为100000-d,给定R个关系,输出一个最低费用,每个关系只能使用一次。
把人看作顶点,关系看作边,就可以转化为无向图中的最大权森林问题,最大权森林问题可以通过把所有边权取反之后用最小生成树的算法求解。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn =;
struct edge {
int u,v,cost;
edge() {}
edge(int a,int b,int c) {
u=a;
v=b;
cost=c;
}
};
bool cmp(const edge& e1,const edge& e2)
{
return e1.cost<e2.cost;
} edge es[];
int par[maxn];
int N,M,R; void init(int n) {
for(int i=;i<n;i++) par[i]=i;
} int find(int x) {
return x==par[x]?x:par[x]=find(par[x]);
} void unite(int x,int y) {
x=find(x);
y=find(y);
if(x!=y) par[x]=y;
} int kruskal() {
sort(es,es+R,cmp);
init(N+M);
int res=;
for(int i=;i<R;i++) {
edge e=es[i];
if(find(e.u)!=find(e.v)) {
unite(e.u,e.v);
res+=e.cost;
}
}
return res;
} int main() {
// freopen("a.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
int x,y,d;
scanf("%d%d%d",&N,&M,&R);
for(int i=;i<R;i++)
{
scanf("%d%d%d",&x,&y,&d);
es[i]=(edge){x,y+N,-d};
//printf("%d %d %d\n",es[i].u,es[i].v,es[i].cost);
}
printf("%d\n",*(N+M)+kruskal());
}
return ;
}
poj - 3723 Conscription(最大权森林)的更多相关文章
- POJ 3723 Conscription(并查集建模)
[题目链接] http://poj.org/problem?id=3723 [题目大意] 招募名单上有n个男生和m个女生,招募价格均为10000, 但是某些男女之间存在好感,则招募的时候, 可以降低与 ...
- POJ 3723 Conscription (Kruskal并查集求最小生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14661 Accepted: 5102 Des ...
- POJ 3723 Conscription MST
http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...
- POJ 3723 Conscription 最小生成树
题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...
- POJ 3723 Conscription
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6325 Accepted: 2184 Desc ...
- POJ 3723 Conscription【最小生成树】
题意: 征用一些男生和女生,每个应都要给10000元,但是如果某个男生和女生之间有关系,则给的钱数为10000减去相应的亲密度,征集一个士兵时一次关系只能使用一次. 分析: kruskal求最小生成树 ...
- kruskal算法:POJ No.3723 Conscription_最小生成树应用_最大权森林
#define _CRT_SECURE_NO_WARNINGS /* 5 5 8 4 3 6831 1 3 4583 0 0 6592 0 1 3063 3 3 4975 1 3 2049 4 2 2 ...
- 【POJ - 3723 】Conscription(最小生成树)
Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ...
- Conscription(POJ 3723)
原题如下: Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16584 Accepted: 57 ...
随机推荐
- Leetcode#92 Reverse Linked List II
原题地址 第一步,找到将要翻转的位置,记录翻转部分前一个节点(prev) 第二步,翻转,记录翻转完成后这部分的首(reverseHead)和尾(reverseTail),以及翻转部分之后的一个节点(p ...
- nginx 杂记
接触nginx一段时间,有些自己的心得,偶尔在网上会看到一些细小的知识点,总结于此 nginx是以多进程的方式来工作的.nginx在启动后,会有一个master进程和多个worker进程. maste ...
- C#中实现VB中的CreateObject方法
经常看到有些VB的例子中直接用个CreateObject就可调用系统功能(大多是COM对象),像用户设定,网络设定等等.虽然C#中可以通过使用VB的命名空间的方法来调用CreateObject函数,但 ...
- explicit构造函数的作用
explicit构造函数是用来防止隐式转换的.请看下面的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...
- Selenium中expected_conditions下text_to_be_present_in_element_value方法的使用
text_to_be_present_in_element: 判断某个元素中的text是否包含了预期的字符串 text_to_be_present_in_element_value: 判断某个元素中的 ...
- HDU 2672 god is a girl (字符串处理,找规律,简单)
题目 //1,1,2,3,5,8,13,21,34,55…… //斐波纳契数列 #include<math.h> #include<stdio.h> #include<s ...
- Android线程消息通信(二)
创建线程消息队列 Android应用程序的消息队列是使用一个MessageQueue对象来描述的,它可以通过调用Looper类的静态成员函数prepareMainLooper或者prepare来创建, ...
- C# 前台线程与后台线程的区别和联系
c#前台线程与后台线程的区别和联系http://www.189works.com/article-13702-1.html 如何取消后台线程的执行http://www.cnblogs.com/shan ...
- 编程实现linux下的shell
/************************************************************************* > File Name: Kris_shel ...
- Codeforces Round #336 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和
B. Hamming Distance Sum Genos needs your help. He was asked to solve the following programming pro ...