[网络流24题]P2774 方格取数

思路和P3355 骑士共存问题基本一样

点击查看代码
#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;
const int maxn = 200 * 200 + 10;
const int N = 200 * 200 + 10;
const int M = N << 5;
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
ll dis[maxn];
bool vis[maxn];
struct node {
    int v;
    ll w;
    int to;
} edge[M * 2];
int pre[N], cnt, dep[N];
int S, T, head[N];
int n, m, q[N], cur[N];
void add(int u, int v, ll w) {
    // cout << u << " " << v << " " << w << endl;
    edge[cnt] = {v, w, head[u]};
    head[u] = cnt++;
    edge[cnt] = {u, 0, head[v]};
    head[v] = cnt++;
}
bool bfs() {
    for (int i = 0; i <= T; i++) dep[i] = 0;
    dep[S] = 1;
    int l = 0, r = 1;
    q[r] = S;
    while (l < r) {
        int u = q[++l];
        for (int i = head[u]; i != -1; i = edge[i].to) {
            int v = edge[i].v;
            if (!dep[v] && edge[i].w) dep[v] = dep[u] + 1, q[++r] = v;
        }
    }
    return dep[T];
}
ll dfs(int u, ll mi) {
    int res = 0;
    if (mi == 0 || u == T) return mi;
    for (int &i = cur[u]; i != -1; i = edge[i].to) {
        int v = edge[i].v;
        if (dep[u] + 1 == dep[v] && edge[i].w) {
            ll minn = dfs(v, min(mi - res, edge[i].w));
            edge[i].w -= minn;
            edge[i ^ 1].w += minn;
            res += minn;
            if (res == mi) return res;
        }
    }
    if (res == 0) dep[u] = 0;
    return res;
}
ll dinic() {
    ll res = 0;
    while (bfs()) {
        memcpy(cur, head, sizeof(head));
        // cout << res << endl;
        res += dfs(S, INF);
    }
    return res;
}
int id(int x, int y) { 
    // cout<<x<<" "<<y<<" "<<(m * (x - 1) + y)<<endl;
    return (m * (x - 1) + y); }
int mp[205][205], f;
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int main() {
    // freopen("C:\\Users\\lenovo\\Downloads\\P2774_3.in", "r", stdin);
    memset(head, -1, sizeof head);
    cin >> n >> m;
    ll ans = 0;
    S = n * m + 1, T = S + 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> mp[i][j];
            ans += mp[i][j];
        }
    }
    // cout << "00";

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            // cout << i << " " << j << endl;
            int now = id(i, j);
            if ((i + j) & 1) {
                add(S, now, mp[i][j]);
            } else {
                add(now, T, mp[i][j]);
                continue;
            }

            for (int k = 0; k < 4; k++) {
                int nx = i + dx[k], ny = j + dy[k];
                if (nx < 1 || nx > n || ny < 1 || ny > m) continue;

                int to = id(nx, ny);
                add(now, to, inf);

                // printf("(%d,%d) :%d\n", i, j, id(i, j));
                // printf("(%d,%d) :%d\n", nx, ny, id(nx, ny));
                // add(now, id(nx, ny), INF);
            }
        }
    }
    // cout << "ok\n";
    ll flow = dinic();
    // cout << flow << endl;
    ans -= flow;
    cout << ans;
    return 0;
}
上一篇:python任务11


下一篇:PIXOR Real-time 3D Object Detection from Point Clouds 中文译文