首页 > 题解 > bzoj 1324 Exca王者之剑

bzoj 1324 Exca王者之剑

Description

Input

第一行给出数字N,M代表行列数.N,M均小于等于100 下面N行M列用于描述数字矩阵

Output

输出最多可以拿到多少块宝石

Sample Input

2 2

1 2

2 1

Sample Output

4

题解

发现只有偶数的时候才会取,要是奇数的时候取,下次就一定取不到了。

那就转换模型,成了只要一个格子取了,和它相邻的格子就不能取了。

将格子黑白染色,白点连S,黑点连T。而且对于每个白点,和它不能同时取的连上inf的边,表示不能同时割。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 400030
using namespace std;
const int inf = 0x3f3f3f3f;
struct flows {
    struct node {
        int to, next, flow;
    } e[N];
    int tot, st[N], dis[N], cur[N];
    void init() {
        tot = -1, memset(e, -1, sizeof e),
        memset(st, -1, sizeof st);
    }
    void add(int x, int y, int z) {
        e[++tot].to = y;
        e[tot].flow = z;
        e[tot].next = st[x];
        st[x] = tot;
    }
    void add_edge(int x, int y, int z) {
//      printf("add%d %d %d\n", x, y, z);
        add(x, y, z), add(y, x, 0);
    }
    queue <int> que;
    int bfs(int S, int T) {
        memcpy(cur, st, sizeof st);
        memset(dis, 0, sizeof dis);
        while (!que.empty()) que.pop();
        que.push(S);
        dis[S] = 1;
        while(!que.empty()) {
            int now = que.front();
            que.pop();
            for (int i = st[now]; ~i; i = e[i].next)
                if (e[i].flow && !dis[e[i].to]) {
                    dis[e[i].to] = dis[now] + 1;
                    if (e[i].to == T) return 1;
                    que.push(e[i].to);
                }
        }
        return 0;
    }
    int finds(int now, int T, int flow) {
        if (now == T)
            return flow;
        int f;
        for (int i = cur[now]; ~i; i = e[i].next) {
            cur[now] = i;
            if (e[i].flow && dis[e[i].to] == dis[now] + 1 &&
                (f = finds(e[i].to, T, min(flow, e[i].flow)))) {
                e[i].flow -= f;
                e[i^1].flow += f;
                return f;
            }
        }
        return 0;
    }
    int dinic(int S, int T) {
        int flow = 0, x;
        while(bfs(S, T)) {
            while(x = finds(S, T, inf)) {
                flow += x;
            }
        }
        return flow;
    }
}flow;
int n, m, x, ans;
int ge(int x, int y) { return (x - 1) * m + y; }
main() {
    scanf("%d%d", &n, &m);
    int S = 0, T = n * m + 1;
    flow.init();
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) {
            scanf("%d", &x), ans += x;
            if ((i & 1) && (j & 1) || !(i & 1) && !(j & 1))
                flow.add_edge(S, ge(i, j), x);
            else
                flow.add_edge(ge(i, j), T, x);
        }
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            if ((i & 1) && (j & 1) || !(i & 1) && !(j & 1)) {
                if (i > 1) flow.add_edge(ge(i, j), ge(i - 1, j), inf);
                if (j > 1) flow.add_edge(ge(i, j), ge(i, j - 1), inf);
                if (i < n) flow.add_edge(ge(i, j), ge(i + 1, j), inf);
                if (j < m) flow.add_edge(ge(i, j), ge(i, j + 1), inf);
            }
    printf("%d\n", ans - flow.dinic(S, T));
}