[网络流24题]P4012 深海机器人

费用流,拆边。阴间输入。
https://www.luogu.com.cn/problem/P4012

题意

有一个网格和一些能向上或者向右行走的机器人,机器人第一次通过网格边时可以获得奖励,但是之后到的机器人不会得到奖励(即每个奖励只能被获得一次),有一些机器人起点和能容纳一定数量机器人的终点,问机器人走完之后最多能获得多少奖励?

Tutorial

不难想到最大费用流,需要稍稍处理一下奖励问题:
将每个奖励拆成两条边:
一条容量为1,费用为奖励数;
一条容量为inf,费用为0.

spfa找最长路,这样就保证了只有第一个通过的能得到奖励,并且一定优先走有奖励的那条边。

图建起来是比较奇怪的,有两行要反过来。为了确保建出了想要的图,一些傻瓜式debug还是需要的,比如:
printf("(%d, %d)->(%d, %d)\n", j-1, i, j, i);

点击查看代码
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
#define endl '\n'
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define P pair<int, int>
#define endl '\n'
using namespace std;

#define int long long
typedef long long ll;
const int maxn = 16 * 16 * 2 + 10;
const ll inf = 1e18;
int n1, n2, cnt_edge = 1, S, T;
int head[maxn];
ll dis[maxn];
bool vis[maxn];
struct edge {
    int to, nxt;
    ll flow, cost;
} e[(maxn * maxn) << 2];
inline void add(int u, int v, ll w, ll c) {
    e[++cnt_edge].nxt = head[u];
    head[u] = cnt_edge;
    e[cnt_edge].to = v;
    e[cnt_edge].flow = w;
    e[cnt_edge].cost = c;
}
inline void addflow(int u, int v, ll w, ll c) {
    add(u, v, w, c);
    add(v, u, 0, -c);
}
inline bool spfa(int on) {
    memset(vis, 0, sizeof(vis));
    if (on == 1)
        for (int i = 0; i <= T; i++) dis[i] = inf;
    else
        for (int i = 0; i <= T; i++) dis[i] = -inf;

    queue<int> q;
    q.push(S);
    dis[S] = 0;
    vis[S] = 1;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        vis[x] = 0;
        for (int i = head[x]; i; i = e[i].nxt) {
            int y = e[i].to;
            // cout << "->" << y << endl;
            if ((on == 1 && e[i].flow && dis[y] > dis[x] + e[i].cost) ||
                (on == -1 && e[i].flow && dis[y] < dis[x] + e[i].cost)) {
                dis[y] = dis[x] + e[i].cost;
                if (!vis[y]) q.push(y), vis[y] = 1;
            }
        }
    }
    if (on == 1)
        return dis[T] != inf;
    else
        return dis[T] != -inf;
}
ll dfs(int x, ll lim) {
    vis[x] = 1;
    if (x == T || lim <= 0) return lim;
    ll res = lim;
    for (int i = head[x]; i; i = e[i].nxt) {
        int y = e[i].to;
        if (dis[y] != dis[x] + e[i].cost || e[i].flow <= 0 || vis[y]) continue;
        ll tmp = dfs(y, min(res, e[i].flow));
        res -= tmp;
        e[i].flow -= tmp;
        e[i ^ 1].flow += tmp;
        if (res <= 0) break;
    }
    return lim - res;
}
inline ll Dinic(int on) {
    ll res = 0, cost = 0;
    while (spfa(on)) {
        ll flow = dfs(S, inf);
        res += flow, cost += flow * dis[T];
    }
    return cost;
}
int id(int x, int y, int in) {
    return 2 * ((n1 * 2 + x - 2) * (x - 1) / 2 + y - 1) + in;
}
int p, q;

// int cnt = 0;
int id(int x, int y) {
    // if(cnt) cout << x << " " << y << " :" << (q+1) * (y) + x << endl;
    // cnt ^= 1;
    return (q + 1) * (y) + x;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n1 >> n2 >> p >> q;
    S = (p + 1) * (q + 1), T = S + 1;
    int m;
    for (int i = 0; i <= p; i++) {
        for (int j = 1; j <= q; j++) {
            cin >> m;
            // printf("(%d, %d)->(%d, %d)\n", j-1, i, j, i);
            addflow(id(j - 1, i), id(j, i), 1, m);
            addflow(id(j - 1, i), id(j, i), inf, 0);
        }
    }
    for (int i = 0; i <= q; i++) {
        for (int j = 1; j <= p; j++) {
            cin >> m;
            // printf("(%d, %d)->(%d, %d)\n", i, j-1, i, j);
            addflow(id(i, j - 1), id(i, j), 1, m);
            addflow(id(i, j - 1), id(i, j), inf, 0);
        }
    }

    for (int i = 0, k, x, y; i < n1; i++) {
        cin >> k >> x >> y;
        swap(x, y);
        // printf("S->(%d, %d)\n", x, y);
        addflow(S, id(x, y), k, 0);
    }

    for (int i = 0, k, x, y; i < n2; i++) {
        cin >> k >> x >> y;
        swap(x, y);
        // printf("(%d, %d)->T\n", x, y);
        addflow(id(x, y), T, k, 0);
    }

    // cout << S << " " << T << endl;
    cout << Dinic(-1);
    return 0;
}
上一篇:[网络流24题]P3358 最长k可重区间


下一篇:【模板】欧拉反演和莫比乌斯反演