[网络流24题]P3356 火星探险

一道普通费用流,需要最终方法
https://www.luogu.com.cn/problem/P3356

其他的正常写就行了,说一说输出方案。
从源点开始dfs,如果这条边走了,即反向边非0,则这条边在方案里。
记得用e[i].flow += 1; e[i ^ 1].flow -= 1;把这条边去掉。

点击查看代码
#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;

typedef long long ll;
const int maxn = 35 * 35 * 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;
            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 p, q;

int id(int x, int y, int k) {
    return (q * (x - 1) +y) + k*p*q;
}
int mp[40][40], f;
void dfs_path(int p, int x, int y) {
    int now = id(x, y, 1);
    if (x == ::p && y == q) f = 1;
    if (f) return;
    
    for (int i = head[now]; i; i = e[i].nxt) {
        if (f) return;
        int v = e[i].to;
        if (e[i ^ 1].flow == 0 || f) continue;
        int nx = v / q + 1, ny = v % q;
        if (ny == 0) ny = q,nx--;
        if (nx == x + 1 && ny == y) {//↓
            cout << p << " " << 0 << endl;

            e[i].flow += 1;
            e[i ^ 1].flow -= 1;
            dfs_path(p, nx, ny);
            if (f) return;
        }
        else if (x == nx && y + 1 == ny) {//→
            cout << p << " " << 1 << endl;
            e[i].flow += 1;
            e[i ^ 1].flow -= 1;
            dfs_path(p, nx, ny);
            if (f) return;
        }
    }
}
int main() {
    int k;
    cin >> k >> q >> p;
    S = 0, T = 2 * p * q + 1;
    addflow(S, id(1, 1, 0), k, 0);
    addflow(id(p, q, 1), T, k, 0);

    for (int i = 1; i <= p; i++) {
        for (int j = 1; j <= q; j++) {
            cin >> mp[i][j];

            addflow(id(i, j, 0), id(i, j, 1), inf, 0);
            if (mp[i][j] == 2) {
                addflow(id(i, j, 0), id(i, j, 1), 1, 1);
            }
        }
    }
    int dx[] = {0, 1};
    int dy[] = {1, 0};
    for (int i = 1; i <= p; i++) {
        for (int j = 1; j <= q; j++) {
            if (mp[i][j] == 1) continue;

            for (int k = 0; k < 2; k++) {
                int nx = i + dx[k], ny = j + dy[k];
                if (nx < 1 || nx > p || ny < 1 || ny > q || mp[nx][ny] == 1)
                    continue;
                addflow(id(i, j, 1), id(nx, ny, 0), inf, 0);
            }
        }
    }
    Dinic(-1);
    for (int i = 1; i <= k; i++) {
        f = 0;
        dfs_path(i, 1, 1);
    }
    return 0;
}
posted @ 2021-11-11 15:00  FushimiYuki  阅读(51)  评论(0)    收藏  举报