1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include<bits/stdc++.h> using LL = long long;
constexpr int INF = 0x3f3f3f3f; template< typename T > struct Edge { int from, to; T cap, flow;
Edge(int u, int v, T c, T f) : from(u), to(v), cap(c), flow(f) {} }; template< typename T > struct Dinic { int n, m; int Source, Sink; std::vector< Edge< T > > edges; std::vector< std::vector< int > > G; std::vector< int > d, cur; std::vector< bool > vis;
Dinic(int n) : n(n), d(n), cur(n), vis(n), G(n) {} void AddEdge(int from, int to, T cap) { edges.push_back(Edge< T >(from, to, cap, 0)); edges.push_back(Edge< T >(to, from, 0, 0)); m = edges.size(); G[from].push_back(m - 2); G[to].push_back(m - 1); }
bool BFS() { vis.assign(n, 0); std::queue< int > Q; Q.push(Source); d[Sink] = 0; vis[Source] = true; while (!Q.empty()) { int x = Q.front(); Q.pop(); for (int i = 0; i < G[x].size(); ++ i) { Edge< T > &e = edges[G[x][i]]; if (!vis[e.to] && e.cap > e.flow) { vis[e.to] = true; d[e.to] = d[x] + 1; Q.push(e.to); } } } return vis[Sink]; }
T DFS(int x, T a) { if (x == Sink || a == 0) { return a; } T flow = 0, f; for (int& i = cur[x]; i < G[x].size(); ++ i) { Edge< T > &e = edges[G[x][i]]; if (d[x] + 1 == d[e.to] && (f = DFS(e.to, std::min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[G[x][i] ^ 1].flow -= f; flow += f; a -= f; if (a == 0) { break; } } } return flow; }
T Maxflow(int s, int t) { this->Source = s; this->Sink = t; T flow = 0; while (BFS()) { cur.assign(n, 0); flow += DFS(Source, INF); } return flow; } }; int n, m, c, d; std::string s[300]; LL calc(int k) { Dinic< LL > F(n + n * m + m + 2); int S = n + n * m + m, T = S + 1; for (int i = 0; i < n; ++ i) { for (int j = 0; j < m; ++ j) { if (s[i][j] == '.') { F.AddEdge(n * m + i, i + j * n, 1); F.AddEdge(i + j * n, n * m + n + j, 1); } } } for (int i = 0; i < n; ++ i) { F.AddEdge(S, n * m + i, k); } for (int i = 0; i < m; ++ i) { F.AddEdge(n * m + n + i, T, k); } return F.Maxflow(S, T); } int all = 0; LL f(int num, int k) { return 1LL * (all - num) * d + 1LL * k * c; } signed main() { std::cin.tie(nullptr)->sync_with_stdio(false);
#ifndef ONLINE_JUDGE freopen("Input.in", "r", stdin); freopen("Output.out", "w", stdout); #endif
std::cin >> n >> m >> c >> d; for (int i = 0; i < n; ++ i) { std::cin >> s[i]; for (int j = 0; j < m; ++ j) { if (s[i][j] == '.') { ++ all; } } } int ans; int L = 0, R = n + m; while (L <= R) { int mid = (R - L) / 3, lmid = L + mid, rmid = R - mid;
if (f(calc(lmid), lmid) > f(calc(rmid), rmid)) { L = lmid + 1; } else { R = rmid - 1; ans = rmid; } } std::cout << f(calc(ans), ans) << '\n'; return 0; }
|