結果
問題 | No.421 しろくろチョコレート |
ユーザー |
|
提出日時 | 2022-12-30 15:50:10 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 7 ms / 2,000 ms |
コード長 | 3,698 bytes |
コンパイル時間 | 2,802 ms |
コンパイル使用メモリ | 193,272 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-25 12:20:11 |
合計ジャッジ時間 | 4,683 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 65 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;using pll = pair<ll, ll>;#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)#define rep(i, n) drep(i, 0, n - 1)#define all(a) (a).begin(), (a).end()#define pb push_back#define fi first#define se secondconst ll MOD = 1000000007;const ll MOD2 = 998244353;const ll INF = 1LL << 60;const ll MAX_N = 2e5;struct Dinic{struct Edge{ll to;ll cap;ll rev;};vector<vector<Edge>> G;vector<ll> level;vector<ll> iter;ll N;Dinic(vector<vector<pll>> &G_){N = (ll)G_.size();G.resize(N);level.resize(N);iter.resize(N);rep(from, N){for(pll p : G_[from]){ll to = p.fi;ll cap = p.se;G[from].pb((Edge){to, cap, (ll)G[to].size()});G[to].pb((Edge){from, 0, (ll)G[from].size()-1});}}}void bfs(ll s, vector<vector<Edge>> &H){rep(i, N) level[i]=-1;queue<ll> Q;level[s] = 0;Q.push(s);while(!Q.empty()){ll v = Q.front();Q.pop();for(ll i=0; i<(ll)H[v].size(); i++){Edge &e = H[v][i];if(e.cap > 0 && level[e.to] < 0){level[e.to] = level[v] + 1;Q.push(e.to);}}}}ll dfs(ll v, ll t, ll f, vector<vector<Edge>> &H){if(v == t) return f;for(ll &i=iter[v]; i<(ll)H[v].size(); i++){Edge &e = H[v][i];if(e.cap > 0 && level[v] < level[e.to]){ll d = dfs(e.to, t, min(f, e.cap), H);if(d > 0){e.cap -= d;H[e.to][e.rev].cap += d;return d;}}}return 0;}ll max_flow(ll s, ll t){vector<vector<Edge>> H(N);rep(from, N) for(Edge e : G[from]) H[from].pb(e);ll flow = 0;for(;;){bfs(s, H);if(level[t] < 0) return flow;rep(i, N) iter[i] = 0;ll f;while((f = dfs(s, t, INF, H)) > 0){flow += f;}}}};int main(){cin.tie(nullptr);ios::sync_with_stdio(false);ll h, w;cin >> h >> w;vector<vector<char>> a(h, vector<char>(w));rep(i, h) rep(j, w) cin >> a[i][j];ll n = 0, wn = 0, bn = 0;vector<vector<ll>> alloc(h, vector<ll>(w, -1));rep(i, h) rep(j, w) if((i+j)%2 == 0 && a[i][j] != '.') alloc[i][j]=n++;wn = n;rep(i, h) rep(j, w) if((i+j)%2 == 1 && a[i][j] != '.') alloc[i][j]=n++;bn = n - wn;vector<vector<pll>> G(n+2);ll s = n;ll t = n+1;rep(i, h) rep(j, w){if((i+j)%2 == 0 && alloc[i][j] != -1){G[s].pb({alloc[i][j], 1});}}ll di[4] = {0, 0, 1, -1};ll dj[4] = {1, -1, 0, 0};rep(i, h) rep(j, w){if((i+j)%2 == 1) continue;if(alloc[i][j] == -1) continue;rep(k, 4){ll next_i = i + di[k];ll next_j = j + dj[k];if(next_i < 0 || next_j < 0 || h <= next_i || w <= next_j) continue;if(alloc[next_i][next_j]==-1) continue;G[alloc[i][j]].pb({alloc[next_i][next_j], 1});}}rep(i, h) rep(j, w){if((i+j)%2 == 1 && alloc[i][j] != -1){G[alloc[i][j]].pb({t, 1});}}Dinic dinic(G);ll x, y, z;z = dinic.max_flow(s, t);y = min(wn-z, bn-z);x = n - 2*z - 2*y;cout << x + 10*y + 100*z << endl;}