結果
| 問題 |
No.421 しろくろチョコレート
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2018-02-26 14:57:09 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 2,000 ms |
| コード長 | 3,739 bytes |
| コンパイル時間 | 2,147 ms |
| コンパイル使用メモリ | 184,600 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-23 07:16:23 |
| 合計ジャッジ時間 | 3,718 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 65 |
ソースコード
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
struct BipartiteMatching {
vector<vector<int>> E; int n, m; vector<int> match, dist;
void init(int _n,int _m){n=_n,m=_m;E.resize(n+m+2);match.resize(n+m+2);dist.resize(n+m+2);}
bool bfs() {
queue<int> que;
rep(i, 1, n + 1) {
if (!match[i]) dist[i] = 0, que.push(i);
else dist[i] = inf;
}
dist[0] = inf;
while (!que.empty()) {
int u = que.front(); que.pop();
if (u) fore(v, E[u]) if (dist[match[v]] == inf) {
dist[match[v]] = dist[u] + 1;
que.push(match[v]);
}
}
return (dist[0] != inf);
}
bool dfs(int u) {
if (u) {
fore(v, E[u]) if (dist[match[v]] == dist[u] + 1) if (dfs(match[v])) {
match[v] = u; match[u] = v;
return true;
}
dist[u] = inf;
return false;
}
return true;
}
void add(int a, int b) { b += n; E[a + 1].push_back(b + 1); E[b + 1].push_back(a + 1); }
int whois(int x) { return match[x + 1] - 1; }
int solve() {
rep(i, 0, n + m + 1) match[i] = 0;
int res = 0;
while (bfs()) rep(i, 1, n + 1) if (!match[i] && dfs(i)) res++;
return res;
}
};
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int H, W;
string S[50];
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { -1, 0, 1, 0 };
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> H >> W;
rep(i, 0, H) cin >> S[i];
vector<int> b, w;
rep(y, 0, H) rep(x, 0, W) {
if (S[y][x] == 'b') b.push_back(y * W + x);
if (S[y][x] == 'w') w.push_back(y * W + x);
}
sort(b.begin(), b.end());
sort(w.begin(), w.end());
BipartiteMatching bm;
bm.init(b.size(), w.size());
rep(y, 0, H) rep(x, 0, W) if(S[y][x] == 'b'){
rep(i, 0, 4) {
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || W <= xx) continue;
if (yy < 0 || H <= yy) continue;
if (S[yy][xx] != 'w') continue;
int bb = lower_bound(b.begin(), b.end(), y * W + x) - b.begin();
int ww = lower_bound(w.begin(), w.end(), yy * W + xx) - w.begin();
bm.add(bb, ww);
}
}
int p100 = bm.solve();
int rb = b.size() - p100;
int rw = w.size() - p100;
int p10 = min(rb, rw);
int ans = 100 * p100 + 10 * p10 + (b.size() - p100 - p10) + (w.size() - p100 - p10);
cout << ans << endl;
}
はまやんはまやん