From f9a80c77158e9fdcd0104c38e04728f34afe7bb5 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 11 Jan 2023 21:50:25 +0800 Subject: [PATCH] =?UTF-8?q?2668.=20[cqoi2012]=20=E4=BA=A4=E6=8D=A2?= =?UTF-8?q?=E6=A3=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/bzoj/record/63bebe955b6d7fa6ab4b04d3 --- BZOJ/2668/2668.cpp | 189 ++++++++++++++++++++++++++++++++++++++++++ BZOJ/2668/data/1.in | 3 + BZOJ/2668/data/1.out | 3 + BZOJ/2668/data/10.in | 3 + BZOJ/2668/data/10.out | 3 + BZOJ/2668/data/2.in | 3 + BZOJ/2668/data/2.out | 3 + BZOJ/2668/data/3.in | 3 + BZOJ/2668/data/3.out | 3 + BZOJ/2668/data/4.in | 3 + BZOJ/2668/data/4.out | 3 + BZOJ/2668/data/5.in | 3 + BZOJ/2668/data/5.out | 3 + BZOJ/2668/data/6.in | 3 + BZOJ/2668/data/6.out | 3 + BZOJ/2668/data/7.in | 3 + BZOJ/2668/data/7.out | 3 + BZOJ/2668/data/8.in | 3 + BZOJ/2668/data/8.out | 3 + BZOJ/2668/data/9.in | 3 + BZOJ/2668/data/9.out | 3 + 21 files changed, 249 insertions(+) create mode 100644 BZOJ/2668/2668.cpp create mode 100644 BZOJ/2668/data/1.in create mode 100644 BZOJ/2668/data/1.out create mode 100644 BZOJ/2668/data/10.in create mode 100644 BZOJ/2668/data/10.out create mode 100644 BZOJ/2668/data/2.in create mode 100644 BZOJ/2668/data/2.out create mode 100644 BZOJ/2668/data/3.in create mode 100644 BZOJ/2668/data/3.out create mode 100644 BZOJ/2668/data/4.in create mode 100644 BZOJ/2668/data/4.out create mode 100644 BZOJ/2668/data/5.in create mode 100644 BZOJ/2668/data/5.out create mode 100644 BZOJ/2668/data/6.in create mode 100644 BZOJ/2668/data/6.out create mode 100644 BZOJ/2668/data/7.in create mode 100644 BZOJ/2668/data/7.out create mode 100644 BZOJ/2668/data/8.in create mode 100644 BZOJ/2668/data/8.out create mode 100644 BZOJ/2668/data/9.in create mode 100644 BZOJ/2668/data/9.out diff --git a/BZOJ/2668/2668.cpp b/BZOJ/2668/2668.cpp new file mode 100644 index 00000000..abeec948 --- /dev/null +++ b/BZOJ/2668/2668.cpp @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 25, + M = N * N * 8; +const int INF = 0x3f3f3f3f; + +const int d[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {1, 1}, {1, -1}, {-1, -1}}; + +int n, m, s, t, cnt, cnt1, cnt2, cost; +bool g1[N][N], g2[N][N]; +int idx, head[M], ver[M << 1], next[M << 1]; +std::pair edge[M << 1]; +int dist[M]; +bool vis[M]; + +int id(int x, int y, int k = 0) { + return n * m * k + (x - 1) * m + y; +} + +void add(int u, int v, int c, int w) { + next[idx] = head[u]; + ver[idx] = v; + edge[idx] = std::make_pair(c, w); + head[u] = idx++; +} + +bool spfa() { + std::fill(std::begin(vis), std::end(vis), false); + std::fill(std::begin(dist), std::end(dist), INF); + + std::queue q; + + q.emplace(s); + dist[s] = 0; + vis[s] = true; + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + vis[u] = false; + + for (int i = head[u]; ~i; i = next[i]) { + int v = ver[i], + c = edge[i].first, + w = edge[i].second; + + if (c > 0 && dist[v] > dist[u] + w) { + dist[v] = dist[u] + w; + + if (!vis[v]) { + q.push(v); + vis[v] = true; + } + } + } + } + + return dist[t] != INF; +} + +int dinic(int u, int limit) { + if (u == t) return limit; + + int flow = 0; + + vis[u] = true; + + for (int i = head[u]; ~i && flow < limit; i = next[i]) { + int v = ver[i], + c = edge[i].first, + w = edge[i].second; + + if (dist[v] == dist[u] + w && c && !vis[v]) { + int k = dinic(v, std::min(c, limit - flow)); + + if (!k) dist[v] = INF; + + edge[i].first -= k; + edge[i ^ 1].first += k; + flow += k; + cost += k * w; + } + } + + vis[u] = false; + + 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 = 0, t = id(n, m, 1) + 1; + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + char c; + + cin >> c; + + if (c == '1') { + g1[i][j] = true; + cnt1++; + } + } + } + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + char c; + + cin >> c; + + if (c == '1') { + g2[i][j] = true; + cnt2++; + } + } + } + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (g1[i][j] && !g2[i][j]) { + add(s, id(i, j), 1, 0); + add(id(i, j), s, 0, 0); + cnt++; + } else if (!g1[i][j] && g2[i][j]) { + add(id(i, j, 1), t, 1, 0); + add(t, id(i, j, 1), 0, 0); + } + } + } + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + char c; + + cin >> c; + + int x = c - '0'; + + add(id(i, j), id(i, j, 1), x >> 1, 0); + add(id(i, j, 1), id(i, j), 0, 0); + + if (g1[i][j] != g2[i][j] && x % 2 == 1) { + add(id(i, j), id(i, j, 1), 1, 0); + add(id(i, j, 1), id(i, j), 0, 0); + } + + for (auto d : d) { + int xx = i + d[0], + yy = j + d[1]; + + if (xx < 1 || xx > n || yy < 1 || yy > m) continue; + + add(id(i, j, 1), id(xx, yy), INF, 1); + add(id(xx, yy), id(i, j, 1), 0, -1); + } + } + } + + int res = 0; + + while (spfa()) { + while (int flow = dinic(s, INF)) res += flow; + } + + if (cnt1 != cnt2 || res < cnt) { + cout << -1 << endl; + } else { + cout << cost << endl; + } + + return 0; +} diff --git a/BZOJ/2668/data/1.in b/BZOJ/2668/data/1.in new file mode 100644 index 00000000..3074c5f6 --- /dev/null +++ b/BZOJ/2668/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:86026680661c2b84b741c4a433dfaf2792d37f8b93ca01f1703c2ca2dce0f090 +size 220 diff --git a/BZOJ/2668/data/1.out b/BZOJ/2668/data/1.out new file mode 100644 index 00000000..d4a60b60 --- /dev/null +++ b/BZOJ/2668/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:076320a2a08267b4c026d06573bba408ea68841e73cdc20e62cce59de165ece3 +size 3 diff --git a/BZOJ/2668/data/10.in b/BZOJ/2668/data/10.in new file mode 100644 index 00000000..6327e757 --- /dev/null +++ b/BZOJ/2668/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e44fa114ba02183fe947e1cd7da097522cef9c04eb93589103b68f8c23bda4cc +size 1266 diff --git a/BZOJ/2668/data/10.out b/BZOJ/2668/data/10.out new file mode 100644 index 00000000..f553c3a4 --- /dev/null +++ b/BZOJ/2668/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4c82a221b575ce7fe118b2e8cdf0764bf4ef570a3017e80b6d3438af9095f376 +size 3 diff --git a/BZOJ/2668/data/2.in b/BZOJ/2668/data/2.in new file mode 100644 index 00000000..c599078a --- /dev/null +++ b/BZOJ/2668/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0511a4d71cf0528a3ce433c15ba45d0f974e8df74243081364901099f8bf751b +size 366 diff --git a/BZOJ/2668/data/2.out b/BZOJ/2668/data/2.out new file mode 100644 index 00000000..8da2bb0d --- /dev/null +++ b/BZOJ/2668/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f4ccd05b3271c386ee55d9876c7450012a3b361e5065c09dc22075e38b3cc35c +size 3 diff --git a/BZOJ/2668/data/3.in b/BZOJ/2668/data/3.in new file mode 100644 index 00000000..537cc418 --- /dev/null +++ b/BZOJ/2668/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a37ba673eb87eb8f5909a7ffe314c6310d808f4a53274aba2794785e1ea7a423 +size 636 diff --git a/BZOJ/2668/data/3.out b/BZOJ/2668/data/3.out new file mode 100644 index 00000000..52fa137d --- /dev/null +++ b/BZOJ/2668/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d867d3f88fa20d7cb936ecd93651adeedf473a6177218cd0987f462ff5511cb6 +size 3 diff --git a/BZOJ/2668/data/4.in b/BZOJ/2668/data/4.in new file mode 100644 index 00000000..19f79aa7 --- /dev/null +++ b/BZOJ/2668/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d8131a56fcaf676c130c6cc6ca685642681a3d0426503b410fbe507486d8f22a +size 40 diff --git a/BZOJ/2668/data/4.out b/BZOJ/2668/data/4.out new file mode 100644 index 00000000..ca267b30 --- /dev/null +++ b/BZOJ/2668/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1121cfccd5913f0a63fec40a6ffd44ea64f9dc135c66634ba001d10bcf4302a2 +size 2 diff --git a/BZOJ/2668/data/5.in b/BZOJ/2668/data/5.in new file mode 100644 index 00000000..b3bf3795 --- /dev/null +++ b/BZOJ/2668/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a67604976b3164d9d8ed966e4f55e7b552b1e2eab574e539a12d815e42e52614 +size 76 diff --git a/BZOJ/2668/data/5.out b/BZOJ/2668/data/5.out new file mode 100644 index 00000000..d76a596d --- /dev/null +++ b/BZOJ/2668/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f0b5c2c2211c8d67ed15e75e656c7862d086e9245420892a7de62cd9ec582a06 +size 2 diff --git a/BZOJ/2668/data/6.in b/BZOJ/2668/data/6.in new file mode 100644 index 00000000..b717b501 --- /dev/null +++ b/BZOJ/2668/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:303db97961416239142f1a19ceea406e26ba63858e1277532fbb899e1b801f50 +size 1266 diff --git a/BZOJ/2668/data/6.out b/BZOJ/2668/data/6.out new file mode 100644 index 00000000..187f50fb --- /dev/null +++ b/BZOJ/2668/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e9940468bf18608df53e6a8b660ddd0e14140b2c3e038d2f1b47c60a8d61f41a +size 4 diff --git a/BZOJ/2668/data/7.in b/BZOJ/2668/data/7.in new file mode 100644 index 00000000..c490034e --- /dev/null +++ b/BZOJ/2668/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6ef7a4dca6e2629be793caea9c199d833a7c357933ecb578b1a2f4be9ed18f4e +size 1266 diff --git a/BZOJ/2668/data/7.out b/BZOJ/2668/data/7.out new file mode 100644 index 00000000..376ac20f --- /dev/null +++ b/BZOJ/2668/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f0448ca7d8645d6c1d4902a2c058a37abfc44f1de59ab54163c80faaf72f6a4c +size 4 diff --git a/BZOJ/2668/data/8.in b/BZOJ/2668/data/8.in new file mode 100644 index 00000000..d7e7011b --- /dev/null +++ b/BZOJ/2668/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:123c304e038fd1e036b1e2dfa27d16ebfc460df662ae04241e74c211e8d5e5b0 +size 28 diff --git a/BZOJ/2668/data/8.out b/BZOJ/2668/data/8.out new file mode 100644 index 00000000..311c4506 --- /dev/null +++ b/BZOJ/2668/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ee3aa64bb94a50845d5024cd4bd20202a4567aed5cd5328c0d97e9920775fc28 +size 3 diff --git a/BZOJ/2668/data/9.in b/BZOJ/2668/data/9.in new file mode 100644 index 00000000..29edf78d --- /dev/null +++ b/BZOJ/2668/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fc4b5b141c2bd151869c85f04659c7b131439c3f0c3f616cf10657e54330708b +size 1266 diff --git a/BZOJ/2668/data/9.out b/BZOJ/2668/data/9.out new file mode 100644 index 00000000..327fab49 --- /dev/null +++ b/BZOJ/2668/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2e293834de573754c6a71ea8b66723227db4a68c734bd28851436689c642c7ea +size 5