diff --git a/BZOJ/1001/1001.cpp b/BZOJ/1001/1001.cpp new file mode 100644 index 00000000..57282773 --- /dev/null +++ b/BZOJ/1001/1001.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include + +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 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; +} diff --git a/BZOJ/1001/data/1.in b/BZOJ/1001/data/1.in new file mode 100644 index 00000000..9a6cd21b --- /dev/null +++ b/BZOJ/1001/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d327b3a69a76925d0180aeedb539bd3709f62944b4bf0348356ec38d1c4b7654 +size 3466 diff --git a/BZOJ/1001/data/1.out b/BZOJ/1001/data/1.out new file mode 100644 index 00000000..1afab5f7 --- /dev/null +++ b/BZOJ/1001/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865 +size 2 diff --git a/BZOJ/1001/data/2.in b/BZOJ/1001/data/2.in new file mode 100644 index 00000000..b6769865 --- /dev/null +++ b/BZOJ/1001/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d54d9e7ba2b969cfe662dc127e1dc20263a9c075a7f5e37f707bd4e9b49ec399 +size 54 diff --git a/BZOJ/1001/data/2.out b/BZOJ/1001/data/2.out new file mode 100644 index 00000000..51b03484 --- /dev/null +++ b/BZOJ/1001/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9a92adbc0cee38ef658c71ce1b1bf8c65668f166bfb213644c895ccb1ad07a25 +size 3 diff --git a/BZOJ/1001/data/3.in b/BZOJ/1001/data/3.in new file mode 100644 index 00000000..314d1abc --- /dev/null +++ b/BZOJ/1001/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4e9db814a05481d7c5445f362494d1cbdde64eb384d6855700675642e2d1b8df +size 2983938 diff --git a/BZOJ/1001/data/3.out b/BZOJ/1001/data/3.out new file mode 100644 index 00000000..36b2ebf0 --- /dev/null +++ b/BZOJ/1001/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f8efa325a144dc26e298349bde96890d60a33a5feb9810ace4ef6365ec8265e1 +size 6 diff --git a/BZOJ/1001/data/4.in b/BZOJ/1001/data/4.in new file mode 100644 index 00000000..1c05338b --- /dev/null +++ b/BZOJ/1001/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7c9b444e234703a914bfdd10231a1c7cf68975b5ddf8e0ee1333c399c3f2f0e2 +size 85997 diff --git a/BZOJ/1001/data/4.out b/BZOJ/1001/data/4.out new file mode 100644 index 00000000..1c6ee15b --- /dev/null +++ b/BZOJ/1001/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:95cd69f3fc4d92281bcf3ecb225391ecb04ceaab74c2fd7cf18bc2dfdf5f7502 +size 6 diff --git a/BZOJ/1001/data/5.in b/BZOJ/1001/data/5.in new file mode 100644 index 00000000..fbe1e187 --- /dev/null +++ b/BZOJ/1001/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f9c7b3d128152b7cba212ba6cba243ad31ae9c215db1d1f05855854b3fbe483 +size 7872852 diff --git a/BZOJ/1001/data/5.out b/BZOJ/1001/data/5.out new file mode 100644 index 00000000..92ab7028 --- /dev/null +++ b/BZOJ/1001/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7ffa842bcdc9ae5580846bb8073b7177dfe024604a9a57ee675b73d73ce29f28 +size 4