bzoj 3894 文理分科 最小割

题面

题目传送门

解法

比较经典的最小割问题

  • 这道题可以说和小M的作物几乎是一模一样的
  • 直接建图跑最小割就可以了,建图方式和小M的作物简直就是一模一样

代码

#include <bits/stdc++.h>
#define N 110
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Edge {
    int next, num, c;
} e[N * N * N * 16];
int dx[5] = {0, -1, 1, 0, 0}, dy[5] = {0, 0, 0, -1, 1};
int n, m, s, t, cnt, l[N * N * 3], cur[N * N * 3];
void add(int x, int y, int c) {
    e[++cnt] = (Edge) {e[x].next, y, c};
    e[x].next = cnt;
}
void Add(int x, int y, int c) {
    add(x, y, c), add(y, x, 0);
}
int calc(int x, int y) {return (x - 1) * m + y;}
bool bfs(int s) {
    for (int i = 1; i <= t; i++) l[i] = -1;
    queue <int> q; q.push(s);
    while (!q.empty()) {
        int x = q.front(); q.pop();
        for (int p = e[x].next; p; p = e[p].next) {
            int k = e[p].num, c = e[p].c;
            if (l[k] == -1 && c)
                q.push(k), l[k] = l[x] + 1;
        }
    }
    return l[t] != -1;
}
int dfs(int x, int lim) {
    if (x == t) return lim;
    int used = 0;
    for (int p = cur[x]; p; p = e[p].next) {
        int k = e[p].num, c = e[p].c;
        if (l[k] == l[x] + 1 && c) {
            int w = dfs(k, min(c, lim - used));
            e[p].c -= w, e[p ^ 1].c += w, used += w;
            if (e[p].c) cur[x] = p;
            if (lim == used) return lim;
        }
    }
    if (!used) l[x] = -1; return used;
}
int dinic() {
    int ret = 0;
    while (bfs(s)) {
        for (int i = 0; i <= t; i++) cur[i] = e[i].next;
        ret += dfs(s, INT_MAX);
    }
    return ret;
}
main() {
    read(n), read(m);
    s = 0, t = cnt = 3 * n * m + 1;
    if (cnt % 2 == 0) cnt++; int ans = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            int x = calc(i, j), y; read(y);
            Add(s, x, y); ans += y;
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            int x = calc(i, j), y; read(y);
            Add(x, t, y); ans += y;
        }
    int tot = n * m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            int y; read(y); Add(s, ++tot, y);
            Add(tot, calc(i, j), INT_MAX); ans += y;
            for (int k = 1; k <= 4; k++) {
                int tx = i + dx[k], ty = j + dy[k];
                if (tx <= 0 || ty <= 0 || tx > n || ty > m) continue;
                Add(tot, calc(tx, ty), INT_MAX);
            }
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            int y; read(y); Add(++tot, t, y);
            Add(calc(i, j), tot, INT_MAX); ans += y;
            for (int k = 1; k <= 4; k++) {
                int tx = i + dx[k], ty = j + dy[k];
                if (tx <= 0 || ty <= 0 || tx > n || ty > m) continue;
                Add(calc(tx, ty), tot, INT_MAX);
            }
        }
    cout << ans - dinic() << "\n";
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值