Conscription

Descriptions

需要征募女兵N人,男兵M人。 每招募一个人需要花费10000美元。 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱。 给出若干男女之前的1 ~ 9999 之间的亲密度关系, 招募某个人的费用是 10000 - (已经招募了的人中和自己的亲密度的最大值)。 要求通过适当的招募顺序使得招募所有人所花费的费用最小。

Input

输入N, M, R;
接下来输入R行 (x, y, d) 表示第 x 号男兵和第 y 号女兵之间的亲密度是 d


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

题目链接

https://vjudge.net/problem/POJ-3723

把人看成点,关系看作边,转化为求解无向图的最大权森林问题,这个问题又可以通过把所有边取反之后用最小生成树的算法求解

典型的kruskal算法

注意要用scanf printf

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS \
ios_base::sync_with_stdio(); \
cin.tie();
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x, y) memset(x, y, sizeof(x))
#define Maxn 1000000 + 10
using namespace std;
int N, M, R;
int x[Maxn], y[Maxn], d[Maxn];//男孩 女孩 关系度
struct edge
{
int u, v, cost;//u到v的距离为cost
};
bool cmp(const edge &e1, const edge &e2)//从小到大排序
{
return e1.cost < e2.cost;
}
edge es[Maxn];
int par[Maxn];
void init(int n)//初始化并查集
{
for (int i = ; i <= n; i++)
par[i] = i;
}
int findr(int x)//寻根
{
if (par[x] == x)
return x;
return par[x] = findr(par[x]);
}
void unite(int x, int y)//合并
{
x = findr(x);
y = findr(y);
if (x == y)
return;
par[x] = y;
}
bool same(int x, int y)//判断根是否相同
{
return findr(x) == findr(y);
}
int kruskal(int V, int E)//kruskal算法求最小生成树
{
sort(es, es + E, cmp);
init(V);
int res = ;
for (int i = ; i < E; i++)
{
edge e = es[i];
if (!same(e.u, e.v))
{
unite(e.u, e.v);
res += e.cost;
}
}
return res;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)//T组测试样例
{
scanf("%d%d%d", &N, &M, &R);//存数据
for (int i = ; i < R; i++)
scanf("%d%d%d", &x[i], &y[i], &d[i]);
int V, E;
V = N + M;//顶点个数
E = R;//边个数
for (int i = ; i < E; i++)//存入结构体
{
es[i] = (edge){
x[i], N + y[i], -d[i]};
}
printf("%d\n", * (N + M) + kruskal(V, E));
}
return ;
}
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS                       \
    ios_base::sync_with_stdio(); \
    cin.tie();
#define Mod 
#define eps 1e-
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x, y) memset(x, y, sizeof(x))
#define Maxn  + 
using namespace std;
int N, M, R;
int x[Maxn], y[Maxn], d[Maxn];//男孩 女孩 关系度
struct edge
{
    int u, v, cost;//u到v的距离为cost
};
bool cmp(const edge &e1, const edge &e2)//从小到大排序
{
    return e1.cost < e2.cost;
}
edge es[Maxn];
int par[Maxn];
void init(int n)//初始化并查集
{
    for (int i = ; i <= n; i++)
        par[i] = i;
}
int findr(int x)//寻根
{
    if (par[x] == x)
        return x;
    return par[x] = findr(par[x]);
}
void unite(int x, int y)//合并
{
    x = findr(x);
    y = findr(y);
    if (x == y)
        return;
    par[x] = y;
}
bool same(int x, int y)//判断根是否相同
{
    return findr(x) == findr(y);
}
int kruskal(int V, int E)//kruskal算法求最小生成树
{
    sort(es, es + E, cmp);
    init(V);
    int res = ;
    for (int i = ; i < E; i++)
    {
        edge e = es[i];
        if (!same(e.u, e.v))
        {
            unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)//T组测试样例
    {
        scanf("%d%d%d", &N, &M, &R);//存数据
        for (int i = ; i < R; i++)
            scanf("%d%d%d", &x[i], &y[i], &d[i]);
        int V, E;
        V = N + M;//顶点个数
        E = R;//边个数
        for (int i = ; i < E; i++)//存入结构体
        {
            es[i] = (edge){
                x[i], N + y[i], -d[i]};
        }
        printf("%d\n",  * (N + M) + kruskal(V, E));
    }
    return ;
}

【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【最小生成树】

    题意: 征用一些男生和女生,每个应都要给10000元,但是如果某个男生和女生之间有关系,则给的钱数为10000减去相应的亲密度,征集一个士兵时一次关系只能使用一次. 分析: kruskal求最小生成树 ...

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

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

  5. POJ 3723 Conscription

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

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

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

  7. POJ 3723 Conscription MST

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

  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. LoRaWAN_stack移植笔记(四)__RTC

    stm32相关的配置 由于例程使用的主控芯片为STM32L151C8T6,而在本设计中使用的主控芯片为STM32L051C8T6,内核不一样,并且Cube库相关的函数接口及配置也会有不同,所以芯片的驱 ...

  2. 台式机主机u盘安装centos7报错及注意事项

    利用UltraISO制作U盘启动安装台式机CentOS7系统:流程及报错解决 一.制作U盘 1.首先打开UltraISO软件,尽量下载最新版的 2.点击工具栏中的第二个打开镜像文件工具,如图红色方框标 ...

  3. GSS4&&花仔游历各国

    首先呢,我们想到一种数据结构可以区间开方,一看就不行,但是一看就算是10^18开六次方也只剩一,就不用开根了,所以可以想到用线段树或者分块水过,由于本人 不会用分块,只能用常数巨大的线段树 Code ...

  4. 图灵学院Java架构师-VIP-【性能调优-Mysql索引数据结构详解与索引优化】

    最近报名了图灵学院的架构专题的付费课程,没有赶上6月份开课,中途加入的.错过了多线程的直播课程,只能看录播了

  5. 从0开始学Git——Git的常见操作

    Git常用命令 创建Git版本库 git init [目录]#创建目录版本库, 不写的话是当前目录 该命令会在目录中创建一个名为.git的隐藏目录 文件提交 添加文件: git add 文件名 #添加 ...

  6. python 33 多进程(一)

    目录 多进程 1. 进程创建的两种方式 multiprocessing 2. 进程pid (process id) 3. 验证进程之间的空间隔离 4. 进程对象join方法 5. 进程对象其他属性 6 ...

  7. unity游戏开发_对象池

    现在假设游戏中我们需要实现一个这样功能,按下鼠标左键,发射一颗子弹,3秒之后消失.在这个功能中,我们发射了上百上千发子弹,就需要实例化生成上百上千次.这时候,我们就需要使用对象池这个概念,每次实例化生 ...

  8. Python字典排序问题

    字典的问题 navagation: 1.问题来源 2.dict的学习 *3.numpy的应用 1.问题来源 在做cs231n,assigment1-kNN实现的时候,需要对一个列表中的元素进行计数,并 ...

  9. Windows平台python验证码识别

    参考: http://oatest.dragonbravo.com/Authenticate/SignIn?returnUrl=%2f http://drops.wooyun.org/tips/631 ...

  10. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan(dfs+数学思想)

    题目链接:http://codeforces.com/contest/742/problem/C 题意:题目比较难理解,起码我是理解了好久,就是给你n个位置每个位置标着一个数表示这个位置下一步能到哪个 ...