POJ3723 Conscription

PS: 分析这是一道不错的题目,要求去抽象。

直接说结论: 题目可能是许森林,每一个森林有一颗最小生成树并且选择第一个点花费为10000。

这道题目实现的时候我对0 ~ M进行了编码为 N 到 M+N-1。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 20010;

struct edge {
    int from, to, w;
};

vector<edge> edges;
int f[MAXN];
int N, M, R;
bool cmp(edge x, edge y) {
    if(x.w != y.w) return x.w < y.w;
    else return false;
}

int getFather(int x) {
    if(f[x] == x) return x;
    else return f[x] = getFather(f[x]);
}

int work() {
    const int bound = N+M;
    for(int i = 0; i < bound; i++) f[i] = i;
    sort(edges.begin(), edges.end(), cmp);
    long long tot = 0;
    for(int i = 0; i < (int)edges.size(); i++) {
        edge &t = edges[i];
        int f1 = getFather(t.from);
        int f2 = getFather(t.to);
        if(f1 != f2) {
            f[f1] = f2;
            tot += t.w;
        }
    }
    int cnt = 0;
    for(int i = 0; i < bound; i++) {
        if(f[i] == i) cnt++;
    }
    return 10000*cnt + tot;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d%d", &N, &M, &R);
        edges.clear();
        int x, y, w;
        edge t;
        for(int i = 1; i <= R; i++) {
            scanf("%d%d%d", &x, &y, &w);
            w = 10000-w;
            y = y + N; // encoding.
            t.from = x;
            t.to = y;
            t.w = w;
            edges.push_back(t);
        }
        int res = work();
        printf("%d\n", res);
    }
    return 0;
}

POJ3723 Conscription

时间: 2024-07-31 14:02:22

POJ3723 Conscription的相关文章

POJ3723 Conscription 【并检查集合】

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8071   Accepted: 2810 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

POJ3723 Conscription 【并查集】

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8071   Accepted: 2810 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

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

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

Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ~ 9999 之间的亲密度关系, 招募某个人的费用是 10000 - (已经招募了的人中和自己的亲密度的最大值). 要求通过适当的招募顺序使得招募所有人所花费的费用最小. Input 输入N, M, R;接下来输入R行 (x, y, d) 表示第 x 号男兵和第 y 号女兵之间的亲密度是 d Ou

Conscription

Conscription Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 13   Accepted Submission(s) : 6 Problem Description Windy has a country, and he wants to build an army to protect his country. He has

poj 3723 Conscription(最小生成树拓展)

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7702   Accepted: 2667 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

poj 3723 Conscription 【最大生成树|最大权森林】

题目:poj 3723 Conscription 题意:要征兵n个男兵和m个女兵,每个花费10000元,但是如果已经征募的男士兵中有和将要征募的女士兵关系好的,那么可以减少花费,给出关系,求最小花费. 分析:这个题目初始一个是个二分图,以为可以从这里入手,但是这个题目这个性质没用. 初始花费没人10000,那么减去其中有关系的就是当前的花费. 要是花费最少,那么减去的最大即可,又因为没人只征募一次,即最多选择一个,所以减去一个最大生成树就ok AC代码: #include <cstdio> #

POJ 3723 Conscription (最大权森林 + Kruskal算法)

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8317   Accepted: 2887 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

POJ 3723.Conscription 最小生成树

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13438   Accepted: 4699 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 b