洛谷P3227 [HNOI2013]切糕

前言

我是傻逼

正文

妙妙建图

把每个(x,y)位置上的纵轴看成一条链

如果没有 D D D的限制,直接求出每一纵轴上的最小值之和就行

相当于有一个源点向所有纵轴连容量 inf ⁡ \inf inf的边,
所有纵轴各自连一条长为 R R R的链到汇点,
求最大流\最小割

考虑把 D D D的限制加进网络流

如果一个相邻的纵轴选了高度 i i i
那么当前纵轴低于 i − D i-D iD和高于 i + D i+D i+D的位置都不能选

直接从相邻纵轴的位置 i i i向当前纵轴的位置 i − D i-D iD连边即可
高于 i + D i+D i+D的情况也被从当前纵轴连向相邻纵轴的边包括在内了
之前把 t j > Q tj>Q tj>Q打成 t j > P tj>P tj>P过了19个点 debug半天
这能过19个点 合理吗?

#include
using namespace std;
const int maxn = 1e6+7, inf = 0x3f3f3f3f;
struct edge {
    int v, c, nxt;
}e[maxn*5];
int head[maxn], eid, P, Q, R, D, S, T, d[maxn], tot, cur[maxn];
int id[48][48][48], val[48][48][48], nx[4] = {-1, 0, 1, 0}, ny[4] = {0, -1, 0, 1};
void init() {
    memset(head, -1, sizeof(head));
    eid = 0;
}
void insert(int u, int v, int c) {
    e[eid].v = v; e[eid].nxt = head[u]; e[eid].c = c; head[u] = eid++;
}
bool bfs() {
    memset(d, -1, sizeof(d)); 
    queue<int> q; 
    q.push(S); d[S] = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i = head[u]; ~i; i = e[i].nxt) { 
            int v = e[i].v;
            if (d[v] == -1 && e[i].c) {
                d[v] = d[u]+1; 
                q.push(v);
            }
        }
    }
    return d[T] != -1;
} 
int dfs(int u, int flow) {
    int tmp, res = 0;
    if (u == T) return flow;
    for (int &i = cur[u]; ~i; i = e[i].nxt) {
        int v = e[i].v;
        if (d[v] == d[u] + 1 && e[i].c) {
            tmp = dfs(v, min(flow, e[i].c));
            res += tmp; flow -= tmp; e[i].c -= tmp; e[i^1].c += tmp; 
            if (flow == 0) break;
        }
    }
    if (flow == 0) d[u] = -1;
    return res;
}
int dinic() {
    int res = 0; 
    while (bfs()) {
        memcpy(cur, head, sizeof(cur));
        res += dfs(S, inf);
    } 
    return res;
}
int main() {
    scanf("%d%d%d%d", &P, &Q, &R, &D);
    for (int k = 1; k <= R; k++) {
        for (int i = 1; i <= P; i++) {
            for (int j = 1; j <= Q; j++) {
                id[i][j][k] = ++tot;
                scanf("%d", &val[i][j][k]);
            }
        }
    }
    init();
    S = ++tot; T = ++tot;
    for (int i = 1; i <= P; i++) 
        for (int j = 1; j <= Q; j++) {
            id[i][j][0] = S, id[i][j][R+1] = ++tot;
            insert(id[i][j][R+1], T, inf);
            insert(T, id[i][j][R+1], 0);
        }
    for (int k = 1; k <= R + 1; k++) {
        for (int i = 1; i <= P; i++) {
            for (int j = 1; j <= Q; j++) {
                if (k > 1) {
                    insert(id[i][j][k-1], id[i][j][k], val[i][j][k-1]);
                    insert(id[i][j][k], id[i][j][k-1], 0);
                } else {
                    insert(S, id[i][j][k], inf);
                    insert(id[i][j][k], S, 0);
                }
                if (k > D)
                    for (int t = 0; t < 4; t++) {
                        int ti = i + nx[t], tj = j + ny[t];
                        if (ti < 1 || ti > P || tj < 1 || tj > Q) continue;
                        insert(id[i][j][k], id[ti][tj][k-D], inf);
                        insert(id[ti][tj][k-D], id[i][j][k], 0);
                    }
            }
        }
    }
    printf("%d\n", dinic());
}

你可能感兴趣的:(Oi,最小割,必经边)