POJ1149 PIGS

Posted zzy2005

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1149 PIGS相关的知识,希望对你有一定的参考价值。

题目链接

题解

对于一个人有(k)个钥匙,

然后这(k)个猪圈就等于合并了

那么我们考虑转换问题,

一个人先把(k)个猪圈的猪全买了,再转让给后面的人

具体连边看代码..

Code

#include<cstdio>
#include<queue>
#include<cstring>

#define LL long long
#define RG register

using namespace std;
template<class T> inline void read(T &x) {
    x = 0; RG char c = getchar(); bool f = 0;
    while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
    while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
    x = f ? -x : x;
    return ;
}
template<class T> inline void write(T x) {
    if (!x) {putchar(48);return ;}
    if (x < 0) x = -x, putchar('-');
    int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
    for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
const int N = 2010, inf = 2147483647;
int s, t;

struct node {
    int to, nxt, w;
}g[2000010];
int last[N], gl = 1, cur[N];
void add(int x, int y, int z) {
    g[++gl] = (node) {y, last[x], z};
    last[x] = gl;
    g[++gl] = (node) {x, last[y], 0};
    last[y] = gl;
}

queue<int> q;

int dep[N];

bool bfs() {
    q.push(s);
    memset(dep, 0, sizeof(dep));
    dep[s] = 1;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i = last[u]; i; i = g[i].nxt) {
            int v = g[i].to;
            if (g[i].w > 0 && !dep[v])
                dep[v] = dep[u] + 1, q.push(v);
        }
    }
    return dep[t];
}

int dfs(int u, int d) {
    if (u == t) return d;
    for (int &i = cur[u]; i; i = g[i].nxt) {
        int v = g[i].to;
        if (dep[v] == dep[u]+1 && g[i].w) {
            int di = dfs(v, min(d, g[i].w));
            if (di) {
                g[i].w -= di;
                g[i ^ 1].w += di;
                return di;
            }
        }
    }
    return 0;
}

int dinic() {
    int ans = 0;
    while (bfs()) {
        for (int i = 1; i <= t; i++)
            cur[i] = last[i];
        while (int d = dfs(s, inf))
            ans += d;
    }
    return ans;
}

int p[N], id[N], a[N][N], b[N];

int main() {
    int m, n;
    read(n), read(m);
    s = n + 1; t = s + 1;
    for (int i = 1; i <= n; i++) read(p[i]);
    for (int i = 1; i <= m; i++) {
        read(a[i][0]);
        for (int j = 1; j <= a[i][0]; j++) read(a[i][j]);
        read(b[i]);
    }
    for (int i = 1; i <= m; i++) {
        add(i, t, b[i]);
        for (int j = 1; j <= a[i][0]; j++) {
            if (!id[a[i][j]]) add(s, i, p[a[i][j]]);
            else add(id[a[i][j]], i, inf);
            id[a[i][j]] = i;
        }
    }
    printf("%d
", dinic());
    return 0;
}

以上是关于POJ1149 PIGS的主要内容,如果未能解决你的问题,请参考以下文章

poj1149PIGS——网络最大流

POJ 1149 PIGS

POJ 1149 PIGS

POJ 1149 PIGS

POJ 1149 PIGS

POJ 1149 - PIGS - [最大流构图]