POJ 3723 Conscription【最小生成树】
题意:
征用一些男生和女生,每个应都要给10000元,但是如果某个男生和女生之间有关系,则给的钱数为10000减去相应的亲密度,征集一个士兵时一次关系只能使用一次。
分析:
kruskal求最小生成树,注意男生和女生用偏移量处理。
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct edge{int u, v, w;};
const int maxn = 20050, maxm = 50005;
edge e[maxm];
int n, m ,R;
int pa[maxn], _rank[maxn];
bool cmp(const edge &e1,const edge &e2)
{
return e1.w<e2.w;
}
int _find(int a)
{
if(a == pa[a]) return a;
else return pa[a] = _find(pa[a]);
}
void unite(int a, int b)
{
int ra = _find(a), rb = _find(b);
if(_rank[ra] < _rank[b]) pa[ra] = rb;
else{
pa[rb] = ra;
if(_rank[ra] == _rank[rb]) _rank[ra]++;
}
return;
}
int same(int a, int b)
{
return _find(a) == _find(b);
}
int kruskal(int r)
{
sort(e, e+r, cmp);
for(int i = 0; i < n+m; i++){
pa[i] = i;
}
int res = 0;
for(int i = 0; i < r; i++){
if(!same(e[i].u,e[i].v)){
unite(e[i].u, e[i].v);
res += e[i].w;
}
}
return res;
}
int main (void)
{
int c;scanf("%d",&c);
while(c--){
fill(_rank, _rank + n + m, 1);
scanf("%d%d%d",&n,&m,&R);
int x, y, r;
for(int i = 0; i < R;i++){
scanf("%d%d%d",&x, &y, &r);
e[i] = (edge){x, y + n, -r};
}
printf("%d\n",10000*(n+m) + kruskal(R));
}
}
POJ 3723 Conscription【最小生成树】的更多相关文章
- POJ 3723 Conscription 最小生成树
题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...
- POJ 3723 Conscription (Kruskal并查集求最小生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14661 Accepted: 5102 Des ...
- poj - 3723 Conscription(最大权森林)
http://poj.org/problem?id=3723 windy需要挑选N各女孩,和M各男孩作为士兵,但是雇佣每个人都需要支付10000元的费用,如果男孩x和女孩y存在亲密度为d的关系,只要他 ...
- POJ 3723 Conscription
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6325 Accepted: 2184 Desc ...
- POJ 3723 Conscription(并查集建模)
[题目链接] http://poj.org/problem?id=3723 [题目大意] 招募名单上有n个男生和m个女生,招募价格均为10000, 但是某些男女之间存在好感,则招募的时候, 可以降低与 ...
- POJ 3723 Conscription MST
http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...
- 【POJ - 3723 】Conscription(最小生成树)
Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ...
- POJ 3723 征兵问题(最小生成树算法的应用)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15923 Accepted: 5510 Des ...
- Conscription(POJ 3723)
原题如下: Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16584 Accepted: 57 ...
随机推荐
- vue.js学习参考手册
参考手册 示例:www.51siyuan.cn/161.html
- $.extend(x,y); 函数用法介绍。
第一篇资料: 转自: https://www.cnblogs.com/yuqingfamily/p/5813650.html 语法:jQuery.extend( [deep ], target, o ...
- HTML5应用缓存与Web Workers
1.什么是应用程序缓存 HTML5引入了应用程序缓存,这意味着web应用可进行缓存,并可在没有因特网链接时进行访问. 2.应用缓存的优势 离线浏览 用户可在应用离线时使用它们 ...
- iOS Programming Localization 本地化
iOS Programming Localization 本地化 Internationalization is making sure your native cultural informatio ...
- web页面打印--铺满A4
css <style type="text/css"> body { margin: 0; padding: 0; background-color: #FAFAFA; ...
- 使用Caliburn.Micro系列1:新建项目并引入CM
一.WPF的几个MVVM模式实现 MVVMLight:小众的平民框架,实现简单粗暴. pass:最近更新在15年 官网: http://www.mvvmlight.net/ 最近一篇内容全面的好文: ...
- vim里面搜索字符串
直接在命令模式/+字符串就能搜索到,查找下一个,按“n”
- Webstorm 的 Tab 键调整缩进值
两步即可,注意版本
- Java递归扫描文件路径
import java.io.File; public class Test { public static int count = 0; public static void main(String ...
- 三个层面学playbook(核心)
三个层面学playbook(核心) ansible-playbook是ansible工具中的核心,对比ad-hoc(ansible)命令,可以把playbook理解为一系列动作的组成,结果传递.判断等 ...