题意
Sol
考虑不戴限制的情况,那就是对于每一层连到下一层跑网络流。
考虑戴上添边,不难发现向相邻的点连一条 \(inf\) 边就行了。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <queue>
#define int long long
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
#define fi first
#define se second
const int N = 3e5 + 5, M = 2e6 + 5, inf = 2e18;
namespace G {
array <int, N> fir;
array <int, M> nex, to, cap;
int cnt = 1;
void add(int x, int y, int z) {
cnt++;
nex[cnt] = fir[x];
to[cnt] = y;
cap[cnt] = z;
fir[x] = cnt;
}
void _add(int x, int y, int z) {
add(x, y, z);
add(y, x, 0);
}
}
namespace Mfl {
array <int, N> dis, cur;
queue <int> q;
bool bfs(pii st) {
dis.fill(-1), dis[st.fi] = 0;
q.push(st.fi);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = G::fir[u]; i; i = G::nex[i]) {
if (G::cap[i] <= 0 || ~dis[G::to[i]]) continue;
dis[G::to[i]] = dis[u] + 1; q.push(G::to[i]);
}
}
return ~dis[st.se];
}
int dfs(int x, int augcd, pii st) {
if (x == st.se) return augcd;
int augc = augcd;
for (int &i = cur[x]; i; i = G::nex[i]) {
int flow = G::cap[i];
if (flow <= 0 || dis[G::to[i]] <= dis[x]) continue;
int del = dfs(G::to[i], min(augc, flow), st);
augc -= del;
G::cap[i] -= del, G::cap[i ^ 1] += del;
if (augc <= 0) break;
}
return augcd - (augc < 0 ? 0 : augc);
}
int dinic(pii st) {
int ans = 0;
while (bfs(st)) {
// copy(G::fir.begin(), G::fir.end(), cur.begin());
cur = G::fir;
ans += dfs(st.fi, inf, st);
}
return ans;
}
}
array <array <array <int, 55>, 55>, 55> mp;
int p, q, r;
int _dx[5] = {0, 1, 0, -1, 0}, _dy[5] = {0, 0, 1, 0, -1};
signed main() {
p = read(), q = read(), r = read();
int d = read();
for (int k = 1; k <= r; k++)
for (int i = 1; i <= p; i++)
for (int j = 1; j <= q; j++)
mp[i][j][k] = read();
pii st = make_pair(1, p * q * (r + 1) + 2);
for (int i = 1; i <= p; i++) {
for (int j = 1; j <= q; j++) {
int x = (i - 1) * q + j + 1;
G::_add(st.fi, x, inf);
for (int k = 1; k <= r; k++)
G::_add(p * q * (k - 1) + x, p * q * k + x, mp[i][j][k]);
for (int k = d + 1; k <= r + 1; k++) {
for (int l = 1; l <= 4; l++) {
int _i = i + _dx[l], _j = j + _dy[l];
if (_i < 1 || _j < 1 || _i > p || _j > q) continue;
G::_add(p * q * (k - 1) + (i - 1) * q + j + 1, p * q * (k - d - 1) + (_i - 1) * q + _j + 1, inf);
}
}
G::_add(p * q * r + x, st.se, inf);
}
}
write(Mfl::dinic(st)), puts("");
return 0;
}
标签:切糕,include,int,cnt,st,HNOI2013,add,P3227,define
From: https://www.cnblogs.com/cxqghzj/p/17893215.html