# 【bzoj3144】[Hnoi2013]切糕

## Input

100%的数据满足P,Q,R≤40，0≤D≤R，且给出的所有的不和谐值不超过1000。

2 2 2

1

6 1

6 1

2 6

2 6

6

## 题解

1.S向(x,y,1)连一条容量为f[x][y][1]的边。
2.(x,y,R)向T连一条容量为inf的边。
3.(x,y,z1)(x,y,z)连一条容量为f[x][y][z]的边。
4.若(x,y)(x,y)和相邻，且z>d，则(x,y,z)(x,y,zd)连一条容量为inf的边。

【建模分析】

#include<bits/stdc++.h>using namespace std;inline int read(){    int x = 0, f = 1; char c = getchar();    while(!isdigit(c)) { if(c == '-') f = -1; c = getchar(); }    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }    return x * f;}const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, inf = 0x3f3f3f3f;const int N = 100000 + 10, M = 1000000 + 10;int P, Q, R, D, f[50][50][50];int s, t;struct Edge{    int fr, to, cap, flow;    Edge(){fr = to = flow = cap = 0;}    Edge(int u, int v, int w){fr = u, to = v, cap = w, flow = 0;}}edg[M];int hd[N], nxt[M], tot;int d[N], q[N], vis[N], cur[N], dfn;int ans;void init(){    memset(hd, -1, sizeof(hd));    P = read(); Q = read(); R = read();    D = read();    t = P * Q * R + 1;    for(int i = 1; i <= R; i++)    for(int j = 1; j <= P; j++)    for(int k = 1; k <= Q; k++)        f[i][j][k] = read();}inline int p(int x, int y, int z){    if(!z) return s;    return ((x - 1) * Q + y - 1) * R + z;}inline void insert(int u, int v, int w){    edg[tot] = Edge(u, v, w); nxt[tot] = hd[u]; hd[u] = tot++;    edg[tot] = Edge(v, u, 0); nxt[tot] = hd[v]; hd[v] = tot++;}void build(){    for(int i = 1; i <= P; i++)        for(int j = 1; j <= Q; j++){            for(int k = 1; k <= R; k++){                insert(p(i, j, k-1), p(i, j, k), f[k][i][j]);                if(k > D)                for(int l = 0; l < 4; l++){                    int x = i + dx[l], y = j + dy[l];                    if(1 <= x && x <= P && 1 <= y && y <= Q)                        insert(p(i, j, k), p(x, y, k - D), inf);                }            }            insert(p(i, j, R), t, inf);        }}bool bfs(){    int head = 0, tail = 1;    d[s] = 0, q[0] = s, vis[s] = ++dfn;    while(head < tail){        int u = q[head++];        for(int i = hd[u]; i >= 0; i = nxt[i]){            Edge &e = edg[i];            if(vis[e.to] != dfn && e.cap > e.flow){                d[e.to] = d[u] + 1;                vis[e.to] = dfn;                q[tail++] = e.to;            }        }    }    return vis[t] == dfn;}int dfs(int x, int a){    if(x == t || a == 0) return a;    int flow = 0, f;    for(int &i = cur[x]; i >= 0; i = nxt[i]){        Edge &e = edg[i];        if(d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){            flow += f;            e.flow += f;            a -= f;            edg[i^1].flow -= f;            if(a == 0) break;        }    }    return flow;}void work(){    build();    while(bfs()){        for(int i = s; i <= t; i++) cur[i] = hd[i];        ans += dfs(s, inf);    }    printf("%d\n", ans);}int main(){    init();    work();    return 0;}
0 0