結果
問題 | No.421 しろくろチョコレート |
ユーザー |
![]() |
提出日時 | 2016-09-17 00:58:29 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 7 ms / 2,000 ms |
コード長 | 2,818 bytes |
コンパイル時間 | 723 ms |
コンパイル使用メモリ | 69,764 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-23 07:08:37 |
合計ジャッジ時間 | 2,220 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 65 |
ソースコード
#include <iostream>#include <vector>#include <cstring>using namespace std;const int MAX_N = 60;struct BipartiteMatching {int V; // 頂点数vector<vector<int> > graph; // グラフの隣接リスト表現vector<int> match; // 各頂点ごとのマッチングの対応vector<bool> used; // DFSの際に使用BipartiteMatching(int v) {V = v;graph = vector<vector<int> >(V);match = vector<int>(V);used = vector<bool>(V);}// uとvを連結にするvoid AddEdge(int u, int v) {graph[u].push_back(v);graph[v].push_back(u);}bool DFS(int v) {used[v] = true;for (int i = 0; i < graph[v].size(); i++) {int u = graph[v][i], w = match[u];if (w < 0 || (!used[w] && DFS(w))) {match[v] = u;match[u] = v;return true;}}return false;}// 二部マッチングを解くint Solve() {int res = 0;fill(match.begin(), match.end(), -1);for (int v = 0; v < V; v++) {if (match[v] < 0) {fill(used.begin(), used.end(), false);if (DFS(v)) {res++;}}}return res;}};int n, m;int conv_id(int row, int col) {return row * m + col;}string s[MAX_N];int main() {cin >> n >> m;BipartiteMatching bm(n * m);int w_num = 0, b_num = 0;for (int r = 0; r < n; r++) {cin >> s[r];for (int c = 0; c < m; c++) {if (s[r][c] == 'w') w_num++;else if (s[r][c] == 'b') b_num++;}}for (int r = 0; r < n; r++) {for (int c = 0; c < m; c++) {if (s[r][c] == 'w') {if (r - 1 >= 0 && s[r - 1][c] == 'b') {bm.AddEdge(conv_id(r, c), conv_id(r - 1, c));}if (r + 1 < n && s[r + 1][c] == 'b') {bm.AddEdge(conv_id(r, c), conv_id(r + 1, c));}if (c - 1 >= 0 && s[r][c - 1] == 'b') {bm.AddEdge(conv_id(r, c), conv_id(r, c - 1));}if (c + 1 < m && s[r][c + 1] == 'b') {bm.AddEdge(conv_id(r, c), conv_id(r, c + 1));}}}}int matching = bm.Solve(),score3 = matching * 100,score2 = min(w_num - matching, b_num - matching) * 10,score1 = (w_num + b_num) - matching * 2 - score2 / 10 * 2;cout << score1 + score2 + score3 << endl;/*cout << "w: " << w_num << " " << "b: " << b_num << endl;cout << score1 << " " << score2 << " " << score3 << endl;*/return 0;}