• 原题如下:

    Conscription
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 16584   Accepted: 5764

    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, NM and R.
    Then R lines followed, each contains three integers xiyi and di.
    There is a blank line before each test case.

    1 ≤ NM ≤ 10000
    0 ≤ R ≤ 50,000
    0 ≤ xi < N
    0 ≤ yi < M
    0 < di < 10000

    Output

    For each test case output the answer in a single line.

    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
  • 题解:设想这样一个无向图:在征募某个人a时,如果使用了a和b之间的关系,那么就连一条a到b的边,假设这个图中存在圈,那么无论以什么顺序征募这个圈上的所有人,都会产生矛盾,即由于圈的存在,矛盾是必然的,由此可以知道,这个图应该是一片森林。反过来,给定一片森林里,必然可以使用对应的关系确定征募的顺序。综上,把人看作点,关系看作边,这个问题就可以转化为求解无向图中的最大权森林问题,最大权森林问题可以通过把所有边权取反后用最小生成树的算法求解
  • 代码:
     #include <cstdio>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #include <cctype>
    #define num s-'0' using namespace std; struct edge
    {
    int u;
    int v;
    int cost;
    }; const int MAX_V=;
    const int MAX_E=;
    const int INF=0x3f3f3f3f;
    int K,N, M, E, V;
    edge es[MAX_E];
    long long res;
    int par[MAX_V];
    int r[MAX_V]; void kruskal();
    void init();
    int find(int);
    void unite(int, int);
    bool same(int, int); void read(int &x){
    char s;
    x=;
    bool flag=;
    while(!isdigit(s=getchar()))
    (s=='-')&&(flag=true);
    for(x=num;isdigit(s=getchar());x=x*+num);
    (flag)&&(x=-x);
    } void write(int x)
    {
    if(x<)
    {
    putchar('-');
    x=-x;
    }
    if(x>)
    write(x/);
    putchar(x%+'');
    } bool compare(const edge &e1, const edge &e2)
    {
    return e1.cost<e2.cost;
    } int main()
    {
    read(K);
    for (int j=; j<K; j++)
    {
    res=;
    read(N);read(M);read(E);V=N+M;
    for (int i=; i<E; i++)
    {
    read(es[i].u);read(es[i].v);read(es[i].cost);
    es[i].v+=N;es[i].cost=-es[i].cost;
    }
    kruskal();
    write(*(N+M)+res);
    putchar('\n');
    }
    } void init()
    {
    for (int i=; i<V; i++)
    {
    par[i]=i;
    r[i]=;
    }
    } int find(int x)
    {
    if (par[x]==x) return x;
    return par[x]=find(par[x]);
    } void unite(int x, int y)
    {
    x=find(x);
    y=find(y);
    if (x==y) return;
    if (r[x]<r[y]) par[x]=y;
    else
    {
    par[y]=x;
    if (r[x]==r[y]) r[x]++;
    }
    } bool same(int x, int y)
    {
    return (find(x)==find(y));
    } void kruskal()
    {
    sort(es, es+E, compare);
    init();
    for (int i=; i<E; i++)
    {
    edge e=es[i];
    if (!same(e.u, e.v))
    {
    unite(e.u, e.v);
    res+=e.cost;
    }
    }
    }

Conscription(POJ 3723)的更多相关文章

  1. MST:Conscription(POJ 3723)

      男女搭配,干活不累 题目大意:需要招募女兵和男兵,每一个人都的需要花费1W元的招募费用,但是如果有一些人之间有亲密的关系,那么就会减少一定的价钱,如果给出1~9999的人之间的亲密关系,现在要你求 ...

  2. Day5 - D - Conscription POJ - 3723

    Windy has a country, and he wants to build an army to protect his country. He has picked up N girls ...

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

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

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

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

  5. POJ 3723 Conscription MST

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

  6. POJ 3723 Conscription 最小生成树

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

  7. POJ 3723 Conscription

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

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

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

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

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

随机推荐

  1. 2w字 + 40张图带你参透并发编程!

    并发历史 在计算机最早期的时候,没有操作系统,执行程序只需要一种方式,那就是从头到尾依次执行.任何资源都会为这个程序服务,在计算机使用某些资源时,其他资源就会空闲,就会存在 浪费资源 的情况. 这里说 ...

  2. centos7 + nginx+django 运行环境

    .easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安装easy_install 下载地址:https://pypi.python.org/pypi ...

  3. Linux环境下安装JDK8

    Linux环境下搭建Java项目运行环境,首先要安装JDK,安装JDK8的步骤如下: 1 下载JDK安装包 下载地址:http://www.oracle.com/technetwork/java/ja ...

  4. 从一个小需求感受Redis的独特魅力

    分享一个简单的小需求应该怎么设计实现以及有关Redis的使用 Redis在实际应用中使用的非常广泛,本篇文章就从一个简单的需求说起,为你讲述一个需求是如何从头到尾开始做的,又是如何一步步完善的. 需求 ...

  5. Django万能权限框架组件

    业务场景分析 假设我们在开发一个培训机构的 客户关系管理系统,系统分客户管理.学员管理.教学管理3个大模块,每个模块大体功能如下 客户管理 销售人员可以录入客户信息,对客户进行跟踪,为客户办理报名手续 ...

  6. 学习lammps 对in文件的一个概述性心得(转载)

    转载自:http://muchong.com/html/201411/8149677.html 写在开头:1.尽量列举了大部分(几乎)的命令2.带星号命令非常重要,大家在看mannual中命令的解释的 ...

  7. 区块链入门到实战(32)之Solidity – 代码注释

    Solidity 支持c风格和c++风格的注释. //之后到行尾的文本,都被看作注释,编译器忽略此内容 /* 与 */ 之间的文本被看作注释, 编译器忽略此内容 示例 注释示例. function g ...

  8. 程序员小哥教你秋招拿大厂offer

    快要到秋招了,对于应届生来说,秋招是一个特别重要的机会.对于社招同学来说,金九银十也是一个很好的跳槽窗口. 而我呢,因为是从上海到广州工作,就没有提前先把工作定下来.刚好也趁这个机会出去旅游了两个月. ...

  9. 华师2019软件专硕复试机试题最后一题G:找数

    G. 找数 单点时限: 1.0 sec 内存限制: 256 MB 问题描述 输入一个整数 n( 2≤n≤10 ) ,你需要找到一些 n 位数(允许有前置 0 ,见样例),这些 n 位数均 由 0 ~ ...

  10. 力扣leetcode 56. 合并区间

    56. 合并区间 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: ...