結果
問題 | No.421 しろくろチョコレート |
ユーザー |
![]() |
提出日時 | 2016-09-09 23:00:35 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 418 ms / 2,000 ms |
コード長 | 1,490 bytes |
コンパイル時間 | 804 ms |
コンパイル使用メモリ | 79,640 KB |
実行使用メモリ | 9,564 KB |
最終ジャッジ日時 | 2024-09-23 07:05:14 |
合計ジャッジ時間 | 4,758 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 65 |
ソースコード
#include <iostream>#include <fstream>#include <vector>#include <cstring>#include <string>#include <algorithm>#include <iomanip>using namespace std;const int MAX_V = 2500;int V;bool G[MAX_V][MAX_V];int match[MAX_V];bool used[MAX_V];bool dfs(int v) {used[v] = true;for(int i = 0; i < V; i++) {if(!G[v][i]) continue;int u = i, w = match[u];if(w < 0 || (!used[w] && dfs(w))) {match[v] = u;match[u] = v;return true;}}return false;}int bipartite_matching() {int res = 0;memset(match, -1, sizeof(match));for(int v = 0; v < V; v++) {if(match[v] < 0) {memset(used, 0, sizeof(used));if(dfs(v)) {res++;}}}return res;}int N, M;int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };string c[50];int toV(int x, int y) {return y * M + x;}int main() {cin.tie(0);ios::sync_with_stdio(false);cin >> N >> M;V = N * M;for(int i = 0; i < N; i++) {cin >> c[i];}int bc = 0, wc = 0;for(int y = 0; y < N; y++) {for(int x = 0; x < M; x++) {if(c[y][x] == '.') continue;if(c[y][x] == 'b') bc++;if(c[y][x] == 'w') wc++;for(int k = 0; k < 4; k++) {int nx = x + dx[k], ny = y + dy[k];if(0 <= nx && nx < M && 0 <= ny && ny < N && c[ny][nx] != '.') {G[toV(x, y)][toV(nx, ny)] = G[toV(nx, ny)][toV(x, y)] = true;}}}}int m = bipartite_matching();bc -= m, wc -= m;int ans = m * 100 + min(bc, wc) * 10 + (max(bc, wc) - min(bc, wc));cout << ans << endl;}