結果

問題 No.421 しろくろチョコレート
ユーザー ikdikd
提出日時 2019-08-21 13:31:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,283 bytes
コンパイル時間 1,088 ms
コンパイル使用メモリ 87,544 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-17 04:38:36
合計ジャッジ時間 6,724 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 WA -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 2 ms
5,376 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 RE -
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 9 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 RE -
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 8 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 4 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 RE -
testcase_37 RE -
testcase_38 AC 12 ms
5,376 KB
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 WA -
testcase_48 AC 22 ms
5,376 KB
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 WA -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 WA -
testcase_59 RE -
testcase_60 AC 14 ms
5,376 KB
testcase_61 AC 5 ms
5,376 KB
testcase_62 RE -
testcase_63 AC 2 ms
5,376 KB
testcase_64 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>
using namespace std;
// ford-fulkerson
template <typename T>
class Graph {
  struct Edge {
    int to, rev;
    T cap;
    Edge(int to, int rev, T cap) : to(to), rev(rev), cap(cap) {}
  };
  vector<vector<Edge>> g;
  T inf;
  vector<int> visited;

public:
  Graph(int n) {
    g.resize(n);
    inf = numeric_limits<T>::max() / 3;
    visited.resize(n, false);
  }
  void add_edge(int from, int to, T cap) {
    g[from].emplace_back(to, g[to].size(), cap);
    g[to].emplace_back(from, (int)g[from].size() - 1, 0);
  }
  T dfs(int i, T curf, int sink) {
    if (i == sink) return curf;
    visited[i] = true;
    for (auto &&e : g[i]) {
      if (visited[e.to] or e.cap == 0) continue;
      auto tmpf = dfs(e.to, min(e.cap, curf), sink);
      if (tmpf > 0) {
        e.cap -= tmpf;
        g[e.to][e.rev].cap += tmpf;
        return tmpf;
      }
    }
    return 0; // 流せなかった
  }
  T ford(int source, int sink) {
    T maxf = 0;
    while (true) {
      fill(visited.begin(), visited.end(), false);
      auto f = dfs(source, inf, sink);
      if (f > 0) {
        maxf += f;
      } else {
        break;
      }
    }
    return maxf;
  }
};

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {

  int n, m;
  cin >> n >> m;
  vector<vector<char>> a(n, vector<char>(m));
  rep(i, n) rep(j, m) cin >> a[i][j];

  Graph<int> g(n * m + 2);
  auto S = n * m, T = S + 1;
  auto index = [&](int i, int j) { return i * n + j; };
  int wh = 0, bk = 0;
  vector<int> dy = {-1, 0, 0, 1}, dx = {0, -1, 1, 0};
  rep(i, n) {
    rep(j, m) {
      if (a[i][j] == 'w') {
        wh += 1;
        g.add_edge(S, index(i, j), 1);
        rep(k, 4) {
          auto ni = i + dy[k], nj = j + dx[k];
          if (0 <= ni and ni < n and 0 <= nj and nj < m) {
            if (a[ni][nj] == 'b') g.add_edge(index(i, j), index(ni, nj), 1);
          }
        }
      } else if (a[i][j] == 'b') {
        bk += 1;
        g.add_edge(index(i, j), T, 1);
      }
    }
  }
  auto match = g.ford(S, T);
  auto ans = match * 100;
  wh -= match;
  bk -= match;
  auto mn = min(wh, bk);
  ans += mn * 10;
  wh -= mn;
  bk -= mn;
  ans += wh + bk;
  cout << ans << endl;

  return 0;
}
0