結果

問題 No.421 しろくろチョコレート
ユーザー nebukuro09nebukuro09
提出日時 2016-11-22 10:09:03
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 2,245 bytes
コンパイル時間 578 ms
コンパイル使用メモリ 100,304 KB
実行使用メモリ 34,192 KB
最終ジャッジ日時 2023-09-02 23:00:42
合計ジャッジ時間 3,186 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 13 ms
19,812 KB
testcase_02 AC 9 ms
18,992 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 7 ms
13,660 KB
testcase_05 AC 3 ms
4,828 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 6 ms
10,992 KB
testcase_08 AC 13 ms
28,820 KB
testcase_09 AC 2 ms
4,368 KB
testcase_10 AC 3 ms
4,368 KB
testcase_11 AC 8 ms
12,360 KB
testcase_12 AC 1 ms
4,372 KB
testcase_13 AC 1 ms
4,372 KB
testcase_14 AC 1 ms
4,368 KB
testcase_15 AC 1 ms
4,368 KB
testcase_16 AC 27 ms
32,868 KB
testcase_17 AC 23 ms
33,168 KB
testcase_18 AC 20 ms
32,960 KB
testcase_19 AC 1 ms
4,372 KB
testcase_20 AC 12 ms
17,276 KB
testcase_21 AC 19 ms
28,280 KB
testcase_22 AC 8 ms
13,564 KB
testcase_23 AC 2 ms
4,368 KB
testcase_24 AC 1 ms
4,368 KB
testcase_25 AC 1 ms
4,368 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 15 ms
33,632 KB
testcase_29 AC 15 ms
32,908 KB
testcase_30 AC 16 ms
33,632 KB
testcase_31 AC 21 ms
33,432 KB
testcase_32 AC 17 ms
33,632 KB
testcase_33 AC 17 ms
33,576 KB
testcase_34 AC 13 ms
34,164 KB
testcase_35 AC 13 ms
34,128 KB
testcase_36 AC 9 ms
14,656 KB
testcase_37 AC 17 ms
30,404 KB
testcase_38 AC 24 ms
33,388 KB
testcase_39 AC 10 ms
17,324 KB
testcase_40 AC 7 ms
12,924 KB
testcase_41 AC 3 ms
4,372 KB
testcase_42 AC 2 ms
4,368 KB
testcase_43 AC 9 ms
16,724 KB
testcase_44 AC 10 ms
16,412 KB
testcase_45 AC 6 ms
13,116 KB
testcase_46 AC 4 ms
6,144 KB
testcase_47 AC 10 ms
17,752 KB
testcase_48 AC 14 ms
19,412 KB
testcase_49 AC 9 ms
19,004 KB
testcase_50 AC 6 ms
13,372 KB
testcase_51 AC 10 ms
15,280 KB
testcase_52 AC 8 ms
12,384 KB
testcase_53 AC 6 ms
12,084 KB
testcase_54 AC 13 ms
29,096 KB
testcase_55 AC 3 ms
4,372 KB
testcase_56 AC 2 ms
4,368 KB
testcase_57 AC 3 ms
5,568 KB
testcase_58 AC 13 ms
19,348 KB
testcase_59 AC 4 ms
5,660 KB
testcase_60 AC 21 ms
34,192 KB
testcase_61 AC 33 ms
33,580 KB
testcase_62 AC 1 ms
4,368 KB
testcase_63 AC 1 ms
4,368 KB
testcase_64 AC 13 ms
33,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
import std.numeric;


class FordFulkerson {
  int N, source, sink;
  int[][] adj;
  int[][] flow;
  bool[] used;

  this(int n, int s, int t) {
    N = n;
    source = s;
    sink = t;
    assert (s >= 0 && s < N && t >= 0 && t < N);
    adj = new int[][](N);
    flow = new int[][](N, N);
    used = new bool[](N);
  }

  void add_edge(int from, int to, int cap) {
    adj[from] ~= to;
    adj[to] ~= from;
    flow[from][to] = cap;
  }

  int dfs(int v, int min_cap) {
    if (v == sink)
      return min_cap;
    if (used[v])
      return 0;
    used[v] = true;
    foreach (to; adj[v]) {
      if (!used[to] && flow[v][to] > 0) {
        auto bottleneck = dfs(to, min(min_cap, flow[v][to]));
        if (bottleneck == 0) continue;
        flow[v][to] -= bottleneck;
        flow[to][v] += bottleneck;
        return bottleneck;
      }
    }
    return 0;
  }

  int run() {
    int ret = 0;
    while (true) {
      foreach (i; 0..N) used[i] = false;
      int f = dfs(source, int.max);
      if (f > 0)
        ret += f;
      else
        return ret;
    }
  }
}

void main() {
  auto input = readln.split.map!(to!int);
  auto H = input[0];
  auto W = input[1];
  auto B = iota(H).map!(_ => readln.chomp).array;

  auto source = H*W, sink = H*W+1;
  auto ff = new FordFulkerson(H*W+2, source, sink);
  int[4] dx = [0, 0, -1, 1];
  int[4] dy = [-1, 1, 0, 0];
  auto count_b = 0, count_c = 0;
  foreach (i; 0..H*W) {
    auto r = i / W;
    auto c = i % W;
    if (B[r][c] == 'b') {
      count_b += 1;
      ff.add_edge(source, i, 1);
      foreach (d; 0..4) {
        auto nr = r + dx[d];
        auto nc = c + dy[d];
        auto j = nr * W + nc;
        if (nr >= 0 && nr < H && nc >= 0 && nc < W && B[nr][nc] == 'w')
          ff.add_edge(i, j, 1);
      }
    }
    else if (B[r][c] == 'w') {
      count_c += 1;
      ff.add_edge(i, sink, 1);
    }
  }

  auto pairs = ff.run();
  auto amari_pairs = min(count_b, count_c) - pairs;
  auto single = max(count_b, count_c) - pairs - amari_pairs;
  writeln(pairs*100 + amari_pairs*10 + single);
}
0