题意:

征用一些男生和女生,每个应都要给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【最小生成树】的更多相关文章

  1. POJ 3723 Conscription 最小生成树

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

  2. POJ 3723 Conscription (Kruskal并查集求最小生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Des ...

  3. poj - 3723 Conscription(最大权森林)

    http://poj.org/problem?id=3723 windy需要挑选N各女孩,和M各男孩作为士兵,但是雇佣每个人都需要支付10000元的费用,如果男孩x和女孩y存在亲密度为d的关系,只要他 ...

  4. POJ 3723 Conscription

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6325   Accepted: 2184 Desc ...

  5. POJ 3723 Conscription(并查集建模)

    [题目链接] http://poj.org/problem?id=3723 [题目大意] 招募名单上有n个男生和m个女生,招募价格均为10000, 但是某些男女之间存在好感,则招募的时候, 可以降低与 ...

  6. POJ 3723 Conscription MST

    http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...

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

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

  8. POJ 3723 征兵问题(最小生成树算法的应用)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15923   Accepted: 5510 Des ...

  9. Conscription(POJ 3723)

    原题如下: Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16584   Accepted: 57 ...

随机推荐

  1. JS 事件添加onclick写法注意。

    自定义函数添加onclick事件写法注意. 错误写法:element.onclick = addclass(className); 正确写法:element.onclick = function(){ ...

  2. hihocoder 分隔相同字符

    思路: 枚举,贪心. 在“合法”的前提下放置越排在前边的字母越好. “合法”:'a' - 'z'中没有一个字母的个数超过当前串剩余长度的一半(偶数情况下)或长度的一半加1(奇数情况下). 实现: #i ...

  3. java.lang.String 字符串操作

    1.获取文件名 //获取文件名,即就是去掉文件的后缀 /** * mypic.jpg * 获取文件名 * 1. 先找到"."的位置 * 2. 从第一个字符开始截取到".& ...

  4. 最实用解决tomcat startup.bat 一闪而过

    1.直接到tomcat 的解压路径中找到log日志,eg:D:\tomcat\apache-tomcat-7.0.73\logs 查看 catalina 这个日志文件,可以清除的定位错误原因:一般可能 ...

  5. linux下php开启pdo扩展

    前提:网页报错 为解决问题:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' 解决方案 : 下载安装PHP_MYSQL扩展 wget http:// ...

  6. 内存管理总结-autoreleasePool

    转自其他 序言 无论是在MRC时期还是ARC时期,做过开发的程序员都接触过autoreleasepool.尽管接触过但本人对它还不是很了解.本文只是将自己的理解说出来.在内存管理的文章中提到了OC的内 ...

  7. MYSQL 查询方法 统计查询 链接查询 子查询

    mysql表格查询方法: 查询: 1.简单查询 select * from Info --查所有数据select Code,Name from Info --查指定列的数据select Code as ...

  8. bat获取注册表值

    @echo off Setlocal enabledelayedexpansion for /f "skip=2 delims=: tokens=1,*" %%i in ('reg ...

  9. python与shell通过微信企业号发送消息

    python与shell通过微信企业号发送信息,脚本来源于网络,做好搬运工,哈哈,相应的参考链接放在末位 shell版本: #!/bin/bash # CropID="xxxx" ...

  10. swift class extension 与继承

    1.扩展中无法继承重写已有函数,不能添加函数. Extensions can add new functionality to a type, but they cannot override exi ...