From 448433277e012c89f4ca739a0526c61544ebad74 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 11 Jan 2023 17:05:02 +0800 Subject: [PATCH] =?UTF-8?q?1189.=20[HNOI2007]=20=E7=B4=A7=E6=80=A5?= =?UTF-8?q?=E7=96=8F=E6=95=A3=20evacuate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/bzoj/record/63be7bb2f49f999f0bd5ce5e --- BZOJ/1189/1189.cpp | 211 ++++++++++++++++++++++++++++++++++++++++++ BZOJ/1189/data/1.in | 3 + BZOJ/1189/data/1.out | 3 + BZOJ/1189/data/10.in | 3 + BZOJ/1189/data/10.out | 3 + BZOJ/1189/data/11.in | 3 + BZOJ/1189/data/11.out | 3 + BZOJ/1189/data/12.in | 3 + BZOJ/1189/data/12.out | 3 + BZOJ/1189/data/2.in | 3 + BZOJ/1189/data/2.out | 3 + BZOJ/1189/data/3.in | 3 + BZOJ/1189/data/3.out | 3 + BZOJ/1189/data/4.in | 3 + BZOJ/1189/data/4.out | 3 + BZOJ/1189/data/5.in | 3 + BZOJ/1189/data/5.out | 3 + BZOJ/1189/data/6.in | 3 + BZOJ/1189/data/6.out | 3 + BZOJ/1189/data/7.in | 3 + BZOJ/1189/data/7.out | 3 + BZOJ/1189/data/8.in | 3 + BZOJ/1189/data/8.out | 3 + BZOJ/1189/data/9.in | 3 + BZOJ/1189/data/9.out | 3 + 25 files changed, 283 insertions(+) create mode 100644 BZOJ/1189/1189.cpp create mode 100644 BZOJ/1189/data/1.in create mode 100644 BZOJ/1189/data/1.out create mode 100644 BZOJ/1189/data/10.in create mode 100644 BZOJ/1189/data/10.out create mode 100644 BZOJ/1189/data/11.in create mode 100644 BZOJ/1189/data/11.out create mode 100644 BZOJ/1189/data/12.in create mode 100644 BZOJ/1189/data/12.out create mode 100644 BZOJ/1189/data/2.in create mode 100644 BZOJ/1189/data/2.out create mode 100644 BZOJ/1189/data/3.in create mode 100644 BZOJ/1189/data/3.out create mode 100644 BZOJ/1189/data/4.in create mode 100644 BZOJ/1189/data/4.out create mode 100644 BZOJ/1189/data/5.in create mode 100644 BZOJ/1189/data/5.out create mode 100644 BZOJ/1189/data/6.in create mode 100644 BZOJ/1189/data/6.out create mode 100644 BZOJ/1189/data/7.in create mode 100644 BZOJ/1189/data/7.out create mode 100644 BZOJ/1189/data/8.in create mode 100644 BZOJ/1189/data/8.out create mode 100644 BZOJ/1189/data/9.in create mode 100644 BZOJ/1189/data/9.out diff --git a/BZOJ/1189/1189.cpp b/BZOJ/1189/1189.cpp new file mode 100644 index 00000000..92130191 --- /dev/null +++ b/BZOJ/1189/1189.cpp @@ -0,0 +1,211 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 25, + M = N * N, + E = 3e5 + 5; +const int INF = 0x3f3f3f3f; + +const int d[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + +int n, m, s, t, cnt, f[M][M]; +char g[N][N]; +int idx, head[E], ver[E << 1], edge[E << 1], next[E << 1]; +int cur[E << 1], dist[E << 1]; +bool vis[E << 1], vis2[M][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::queue q; + + q.emplace(s); + dist[s] = 1; + 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 (!dist[v] && w) { + dist[v] = dist[u] + 1; + cur[v] = head[v]; + + 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; +} + +bool check(int x) { + idx = 0; + std::fill(std::begin(head), std::end(head), -1); + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (g[i][j] == '.') { + add(s, id(i, j), 1); + add(id(i, j), s, 0); + } else if (g[i][j] == 'D') { + for (int k = 0; k < x; k++) { + add(n * m + id(i, j) * x + k, t, 1); + add(t, n * m + id(i, j) * x + k, 0); + } + + for (int k = 0; k < x - 1; k++) { + add(n * m + id(i, j) * x + k, n * m + id(i, j) * x + k + 1, INF); + add(n * m + id(i, j) * x + k + 1, n * m + id(i, j) * x + k, 0); + } + + for (int p = 1; p <= n; p++) { + for (int q = 1; q <= m; q++) { + if (g[p][q] == '.') { + int d = f[id(p, q)][id(i, j)]; + + if (d <= x) { + add(id(p, q), n * m + id(i, j) * x + d - 1, INF); + add(n * m + id(i, j) * x + d - 1, id(p, q), 0); + } + } + } + } + } + } + } + + int res = 0; + + while (bfs()) { + while (int flow = dinic(s, INF)) res += flow; + } + + return res == cnt; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + memset(f, 0x3f, sizeof(f)); + + cin >> n >> m; + + s = 0, t = 10000; + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + cin >> g[i][j]; + + if (g[i][j] == '.') cnt++; + } + } + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (g[i][j] == 'D') { + memset(vis2, 0x00, sizeof(vis2)); + + f[id(i, j)][id(i, j)] = 0; + + std::queue> q; + + q.emplace(i, j); + + while (!q.empty()) { + int x = q.front().first, + y = q.front().second; + q.pop(); + + for (auto d : d) { + int xx = x + d[0], + yy = y + d[1]; + + if (xx < 0 || xx > n || yy < 0 || yy > m || g[xx][yy] != '.' || vis2[xx][yy]) continue; + + vis2[xx][yy] = true; + + if (f[id(xx, yy)][id(i, j)] > f[id(x, y)][id(i, j)] + 1) { + f[id(xx, yy)][id(i, j)] = f[id(x, y)][id(i, j)] + 1; + + q.emplace(xx, yy); + } + } + } + } + } + } + + int l = 0, + r = cnt, + res = -1; + + while (l <= r) { + int mid = (l + r) >> 1; + + if (check(mid)) { + r = mid - 1; + res = mid; + } else { + l = mid + 1; + } + } + + if (res == -1) { + cout << "impossible" << endl; + } else { + cout << res << endl; + } + + return 0; +} diff --git a/BZOJ/1189/data/1.in b/BZOJ/1189/data/1.in new file mode 100644 index 00000000..d8016cc5 --- /dev/null +++ b/BZOJ/1189/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4ff678f9715551fd20674ac0170a30c7a393c6286920d9d47d4bf16d7b73705 +size 163 diff --git a/BZOJ/1189/data/1.out b/BZOJ/1189/data/1.out new file mode 100644 index 00000000..56d7dd00 --- /dev/null +++ b/BZOJ/1189/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f14b4987904bcb5814e4459a057ed4d20f58a633152288a761214dcd28780b56 +size 3 diff --git a/BZOJ/1189/data/10.in b/BZOJ/1189/data/10.in new file mode 100644 index 00000000..1bfc3ec4 --- /dev/null +++ b/BZOJ/1189/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:36231974319cf9ea7b72629f52b490a556e840988a5956aedc44c8704b0ef80f +size 163 diff --git a/BZOJ/1189/data/10.out b/BZOJ/1189/data/10.out new file mode 100644 index 00000000..e9002468 --- /dev/null +++ b/BZOJ/1189/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eea8254c7500ba3de996aa8ad6af399183f04e17d4a8102fde539dbc93a90012 +size 4 diff --git a/BZOJ/1189/data/11.in b/BZOJ/1189/data/11.in new file mode 100644 index 00000000..d5595c2a --- /dev/null +++ b/BZOJ/1189/data/11.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2d3ee0ed1d3618ab9b8a884317a54ffc5a856cf07aa79736d01ab4b1ea937a2c +size 163 diff --git a/BZOJ/1189/data/11.out b/BZOJ/1189/data/11.out new file mode 100644 index 00000000..60f093fb --- /dev/null +++ b/BZOJ/1189/data/11.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a9742eb8ee320e006666aef25ae9aeed948247f3125c9cafa7cf97b7e7467dd5 +size 3 diff --git a/BZOJ/1189/data/12.in b/BZOJ/1189/data/12.in new file mode 100644 index 00000000..0981dcd1 --- /dev/null +++ b/BZOJ/1189/data/12.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:14a0402458e695eea8437cc34db7475335afabaa34eedb299d0e60010fb8407f +size 86 diff --git a/BZOJ/1189/data/12.out b/BZOJ/1189/data/12.out new file mode 100644 index 00000000..c7967cd5 --- /dev/null +++ b/BZOJ/1189/data/12.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:25d4f2a86deb5e2574bb3210b67bb24fcc4afb19f93a7b65a057daa874a9d18e +size 3 diff --git a/BZOJ/1189/data/2.in b/BZOJ/1189/data/2.in new file mode 100644 index 00000000..ae9530f0 --- /dev/null +++ b/BZOJ/1189/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d55b4ed38bbe9ad2a76129455598970480e4b5532c2ad832436ee07faa23fc4 +size 29 diff --git a/BZOJ/1189/data/2.out b/BZOJ/1189/data/2.out new file mode 100644 index 00000000..d595cdb8 --- /dev/null +++ b/BZOJ/1189/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7de1555df0c2700329e815b93b32c571c3ea54dc967b89e81ab73b9972b72d1d +size 2 diff --git a/BZOJ/1189/data/3.in b/BZOJ/1189/data/3.in new file mode 100644 index 00000000..d8b61f71 --- /dev/null +++ b/BZOJ/1189/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ef7e5b0a2d22cf33179b3e8a847988fde4fd5f91c1c5bf5f2825b5ef820b4365 +size 28 diff --git a/BZOJ/1189/data/3.out b/BZOJ/1189/data/3.out new file mode 100644 index 00000000..ca267b30 --- /dev/null +++ b/BZOJ/1189/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1121cfccd5913f0a63fec40a6ffd44ea64f9dc135c66634ba001d10bcf4302a2 +size 2 diff --git a/BZOJ/1189/data/4.in b/BZOJ/1189/data/4.in new file mode 100644 index 00000000..239cc5fc --- /dev/null +++ b/BZOJ/1189/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3590a544e3ceaea8dab1700ddd80bae3982e718ee2f83df0cac150ebd52a14ec +size 111 diff --git a/BZOJ/1189/data/4.out b/BZOJ/1189/data/4.out new file mode 100644 index 00000000..0b703bdd --- /dev/null +++ b/BZOJ/1189/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b58a3da5fde2680191877ec88a1aa7d06927cc3b30cdf0d0db8c39b488891576 +size 3 diff --git a/BZOJ/1189/data/5.in b/BZOJ/1189/data/5.in new file mode 100644 index 00000000..2c6657e6 --- /dev/null +++ b/BZOJ/1189/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:050ed8327b1d74824e6c85d33b865d5930e9df68a51596a5fd9265edad9f2998 +size 163 diff --git a/BZOJ/1189/data/5.out b/BZOJ/1189/data/5.out new file mode 100644 index 00000000..bf7021d6 --- /dev/null +++ b/BZOJ/1189/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e6c21e8d260fe71882debdb339d2402a2ca7648529bc2303f48649bce0380017 +size 3 diff --git a/BZOJ/1189/data/6.in b/BZOJ/1189/data/6.in new file mode 100644 index 00000000..96b6c93b --- /dev/null +++ b/BZOJ/1189/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a4ab79ba426fe293409bbb9c8bc6ca453a9eccbba6f1875919e30e7ff0b9994d +size 163 diff --git a/BZOJ/1189/data/6.out b/BZOJ/1189/data/6.out new file mode 100644 index 00000000..e69a3b92 --- /dev/null +++ b/BZOJ/1189/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2c58f4f087b138a11e9f3f0c5dc615f93da544c25fec688ca9912d439e624eef +size 11 diff --git a/BZOJ/1189/data/7.in b/BZOJ/1189/data/7.in new file mode 100644 index 00000000..44e808a3 --- /dev/null +++ b/BZOJ/1189/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cec75c3de2ffdf9ae0bd4c7f2c48cbc44fbfdfed5dab26dbb757db5e42a3de06 +size 16 diff --git a/BZOJ/1189/data/7.out b/BZOJ/1189/data/7.out new file mode 100644 index 00000000..1afab5f7 --- /dev/null +++ b/BZOJ/1189/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865 +size 2 diff --git a/BZOJ/1189/data/8.in b/BZOJ/1189/data/8.in new file mode 100644 index 00000000..7255026a --- /dev/null +++ b/BZOJ/1189/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f1298b1f744c7fe046404250bf9acd26ee723f0ec84c8431408d39fb0e19a824 +size 163 diff --git a/BZOJ/1189/data/8.out b/BZOJ/1189/data/8.out new file mode 100644 index 00000000..d76a596d --- /dev/null +++ b/BZOJ/1189/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f0b5c2c2211c8d67ed15e75e656c7862d086e9245420892a7de62cd9ec582a06 +size 2 diff --git a/BZOJ/1189/data/9.in b/BZOJ/1189/data/9.in new file mode 100644 index 00000000..bbb0db07 --- /dev/null +++ b/BZOJ/1189/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d9963f023dd77a05f6d4be8e3299f6835e30f4784f1f5407e34b02042eac3504 +size 426 diff --git a/BZOJ/1189/data/9.out b/BZOJ/1189/data/9.out new file mode 100644 index 00000000..35a8a88d --- /dev/null +++ b/BZOJ/1189/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:38a407cd7d38b41132709485046d7817571b73c32433877751399e229ce11ce2 +size 4