結果

問題 No.421 しろくろチョコレート
ユーザー ikdikd
提出日時 2019-08-21 13:32:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 2,283 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 87,772 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:59:01
合計ジャッジ時間 2,512 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 4 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 7 ms
4,348 KB
testcase_18 AC 9 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 5 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 9 ms
4,348 KB
testcase_32 AC 3 ms
4,348 KB
testcase_33 AC 4 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 12 ms
4,348 KB
testcase_38 AC 11 ms
4,348 KB
testcase_39 AC 3 ms
4,348 KB
testcase_40 AC 7 ms
4,348 KB
testcase_41 AC 3 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 11 ms
4,348 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 4 ms
4,348 KB
testcase_48 AC 8 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 13 ms
4,348 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 1 ms
4,348 KB
testcase_58 AC 3 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 14 ms
4,348 KB
testcase_61 AC 6 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 2 ms
4,348 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 * m + 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