mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 14:58:48 +00:00
1001. [BeiJing2006]狼抓兔子
https://hydro.ac/d/bzoj/record/63c3fcfe53dc8ec11aa0d398
This commit is contained in:
parent
b9d084d687
commit
49bf9bcb7c
128
BZOJ/1001/1001.cpp
Normal file
128
BZOJ/1001/1001.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <queue>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 1005,
|
||||
M = N * N;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
|
||||
int n, m, s, t, ans;
|
||||
int idx, head[M], edge[M << 3], ver[M << 3], next[M << 3];
|
||||
int dist[M], cur[M];
|
||||
|
||||
int id(int x, int y) {
|
||||
return (x - 1) * m + y;
|
||||
}
|
||||
|
||||
void add(int u, int v, int w) {
|
||||
next[idx] = head[u];
|
||||
ver[idx] = v;
|
||||
edge[idx] = w;
|
||||
head[u] = idx++;
|
||||
}
|
||||
|
||||
bool bfs() {
|
||||
std::fill(std::begin(dist), std::end(dist), 0);
|
||||
std::copy(std::begin(head), std::end(head), std::begin(cur));
|
||||
|
||||
std::queue<int> q;
|
||||
|
||||
q.emplace(s);
|
||||
dist[s] = 1;
|
||||
|
||||
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 && !dist[v]) {
|
||||
dist[v] = dist[u] + 1;
|
||||
|
||||
if (v == t) return true;
|
||||
|
||||
q.emplace(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]) {
|
||||
cur[u] = i;
|
||||
|
||||
int v = ver[i],
|
||||
w = edge[i];
|
||||
|
||||
if (dist[v] == dist[u] + 1 && w) {
|
||||
int k = dinic(v, std::min(w, limit - flow));
|
||||
|
||||
if (!k) dist[v] = 0;
|
||||
|
||||
edge[i] -= k;
|
||||
edge[i ^ 1] += k;
|
||||
flow += k;
|
||||
}
|
||||
}
|
||||
|
||||
return flow;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
std::fill(std::begin(head), std::end(head), -1);
|
||||
|
||||
cin >> n >> m;
|
||||
|
||||
s = id(1, 1), t = id(n, m);
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1, x; j < m; j++) {
|
||||
cin >> x;
|
||||
|
||||
add(id(i, j), id(i, j + 1), x);
|
||||
add(id(i, j + 1), id(i, j),x);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i < n; i++) {
|
||||
for (int j = 1, x; j <= m; j++) {
|
||||
cin >> x;
|
||||
|
||||
add(id(i, j), id(i + 1, j), x);
|
||||
add(id(i + 1, j), id(i, j), x);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i < n; i++) {
|
||||
for (int j = 1, x; j < m; j++) {
|
||||
cin >> x;
|
||||
|
||||
add(id(i, j), id(i + 1, j + 1), x);
|
||||
add(id(i + 1, j + 1), id(i, j), x);
|
||||
}
|
||||
}
|
||||
|
||||
while (bfs()) {
|
||||
while (int flow = dinic(s, INF)) ans += flow;
|
||||
}
|
||||
|
||||
cout << ans << endl;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
BZOJ/1001/data/1.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1001/data/1.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1001/data/2.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1001/data/2.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1001/data/3.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1001/data/3.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1001/data/4.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1001/data/4.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1001/data/5.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1001/data/5.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1001/data/5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user