0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

#6007. 「网络流 24 题」方格取数

https://loj.ac/s/1418394
This commit is contained in:
Baoshuo Ren 2022-03-21 15:09:35 +08:00
parent 5459ff0f0d
commit 1f92eb9075
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68
23 changed files with 179 additions and 0 deletions

113
LibreOJ/6007/6007.cpp Normal file
View File

@ -0,0 +1,113 @@
#include <cstring>
#include <iostream>
#include <limits>
#include <queue>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1005,
M = 100005;
const int to[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int m, n, u, s, t, flow, ans, sum;
// Graph
int idx, head[N], edge[M << 1], ver[M << 1], next[M << 1];
void add(int u, int v, int w) {
next[idx] = head[u];
ver[idx] = v;
edge[idx] = w;
head[u] = idx++;
}
// Dinic
int d[N], cur[N];
bool bfs() {
memset(d, 0x00, sizeof(d));
std::queue<int> q;
d[s] = 1;
q.push(s);
cur[s] = head[s];
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; ~i; i = next[i]) {
int v = ver[i],
w = edge[i];
if (w && !d[v]) {
d[v] = d[u] + 1;
cur[v] = head[v];
if (v == t) return true;
q.push(v);
}
}
}
return false;
}
int dinic(int u, int limit) {
if (u == t) return limit;
int flow = 0;
for (int &i = cur[u]; ~i && flow < limit; i = next[i]) {
int v = ver[i],
w = edge[i];
if (w && d[v] == d[u] + 1) {
int k = dinic(v, std::min(limit - flow, w));
if (!k) d[v] = 0;
edge[i] -= k;
edge[i ^ 1] += k;
flow += k;
}
}
return flow;
}
int main() {
std::ios::sync_with_stdio(false);
memset(head, 0xff, sizeof(head));
cin >> n >> m;
s = 0, t = n * m + 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int w, u = (i - 1) * m + j;
cin >> w;
sum += w;
if ((i + j) & 1) {
add(s, u, w);
add(u, s, 0);
for (int k = 0; k < 4; k++) {
int xx = i + to[k][0],
yy = j + to[k][1];
if (1 <= xx && xx <= n && 1 <= yy && yy <= m) {
int v = (xx - 1) * m + yy;
add(u, v, std::numeric_limits<int>::max());
add(v, u, 0);
}
}
} else {
add(u, t, w);
add(t, u, 0);
}
}
}
while (bfs()) {
while (flow = dinic(s, std::numeric_limits<int>::max())) ans += flow;
}
cout << sum - ans << endl;
return 0;
}

BIN
LibreOJ/6007/data/grid0.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid0.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6007/data/grid9.out (Stored with Git LFS) Normal file

Binary file not shown.