結果

問題 No.421 しろくろチョコレート
ユーザー alpha_virginisalpha_virginis
提出日時 2016-09-10 01:24:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 4,302 bytes
コンパイル時間 1,746 ms
コンパイル使用メモリ 177,592 KB
実行使用メモリ 56,592 KB
最終ジャッジ日時 2024-09-23 07:08:01
合計ジャッジ時間 4,021 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 20 ms
21,504 KB
testcase_02 AC 9 ms
10,112 KB
testcase_03 AC 5 ms
6,144 KB
testcase_04 AC 8 ms
8,576 KB
testcase_05 AC 8 ms
9,600 KB
testcase_06 AC 5 ms
6,400 KB
testcase_07 AC 14 ms
15,616 KB
testcase_08 AC 8 ms
9,472 KB
testcase_09 AC 7 ms
7,552 KB
testcase_10 AC 7 ms
7,680 KB
testcase_11 AC 14 ms
16,256 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 29 ms
30,976 KB
testcase_17 AC 28 ms
31,488 KB
testcase_18 AC 29 ms
32,000 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 21 ms
23,552 KB
testcase_21 AC 25 ms
26,240 KB
testcase_22 AC 15 ms
16,512 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 18 ms
21,120 KB
testcase_29 AC 22 ms
25,088 KB
testcase_30 AC 19 ms
22,144 KB
testcase_31 AC 37 ms
43,008 KB
testcase_32 AC 21 ms
23,680 KB
testcase_33 AC 21 ms
23,296 KB
testcase_34 AC 5 ms
6,528 KB
testcase_35 AC 5 ms
6,912 KB
testcase_36 AC 14 ms
16,128 KB
testcase_37 AC 29 ms
30,464 KB
testcase_38 AC 34 ms
35,584 KB
testcase_39 AC 12 ms
13,952 KB
testcase_40 AC 19 ms
20,736 KB
testcase_41 AC 9 ms
9,472 KB
testcase_42 AC 7 ms
8,448 KB
testcase_43 AC 13 ms
15,104 KB
testcase_44 AC 26 ms
26,752 KB
testcase_45 AC 8 ms
9,344 KB
testcase_46 AC 8 ms
9,472 KB
testcase_47 AC 17 ms
19,328 KB
testcase_48 AC 34 ms
56,592 KB
testcase_49 AC 16 ms
37,264 KB
testcase_50 AC 9 ms
10,368 KB
testcase_51 AC 24 ms
25,856 KB
testcase_52 AC 14 ms
15,232 KB
testcase_53 AC 7 ms
7,808 KB
testcase_54 AC 9 ms
10,496 KB
testcase_55 AC 7 ms
7,936 KB
testcase_56 AC 7 ms
7,680 KB
testcase_57 AC 8 ms
9,472 KB
testcase_58 AC 18 ms
20,608 KB
testcase_59 AC 10 ms
11,648 KB
testcase_60 AC 38 ms
37,248 KB
testcase_61 AC 37 ms
37,888 KB
testcase_62 AC 3 ms
5,376 KB
testcase_63 AC 2 ms
5,376 KB
testcase_64 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

char field[64][64];
int h, w;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
bool outOfRange(int r, int c) {
  if( not ( 0 <= r and r < h and 0 <= c and c < w ) ) return true;
  return false;
}

const int INF = (1 << 29);
const int maxV = 64 * 64 * 2;
std::vector< std::tuple<int, int> > graph[maxV];
bool finished[maxV];
int flow[maxV][maxV], capacity[maxV][maxV];
int depth[maxV];
int src, dest;
int V, E;

void setupDinic() {
  V = 64 * 64 * 2;
  for(int r = 0; r < h; ++r) {
    for(int c = 0; c < w; ++c) {
      if( field[r][c] == '.' ) continue;
      if( field[r][c] == 'b' ) {
        int s = 64 * 64 - 1;
        int t = 64 * r + c;
        // printf("src to b(%d, %d)\n", r, c);
        graph[s].push_back(std::make_tuple(t, 1));
        graph[t].push_back(std::make_tuple(s, 0));
        capacity[s][t] = 1;
        capacity[t][s] = 0;
        for(int k = 0; k < 4; ++k) {
          int nr = r + dr[k], nc = c + dc[k];
          if( outOfRange(nr, nc) ) continue;
          if( field[nr][nc] == '.' ) continue;
          int s2 = t;
          int t2 = 64 * 64 + 64 * nr + nc;
          // printf("b(%d, %d) to w(%d, %d)\n", r, c, nr, nc);
          graph[s2].push_back(std::make_tuple(t2, 1));
          graph[t2].push_back(std::make_tuple(s2, 0));
          capacity[s2][t2] = 1;
          capacity[t2][s2] = 0;
        }
      }
      if( field[r][c] == 'w' ) {
        int s = 64 * 64 + 64 * r + c;
        int t = 2 * 64 * 64 - 1;
        // printf("w(%d, %d) to dest\n", r, c); 
        graph[s].push_back(std::make_tuple(t, 1));
        graph[t].push_back(std::make_tuple(s, 0));
        capacity[s][t] = 1;
        capacity[t][s] = 0;
      }
    }
  }
        
  // scanf("%d %d", &V, &E);
  // for(int i = 0; i < E; ++i) {
  //   int s, t, c;
  //   scanf("%d %d %d", &s, &t, &c);
  //   graph[s].push_back(std::make_tuple(t, c));
  //   graph[t].push_back(std::make_tuple(s, 0));
  //   capacity[s][t] = c;
  //   capacity[t][s] = 0;
  // }
  src = 64 * 64 - 1;
  dest = 2 * 64 * 64 - 1;
}

// update depth (foreach, update depth[next])
void bfs() {
  for(int i = 0; i < V; ++i) {
    depth[i] = INF;
  }
  depth[src] = 0;
  int limitdepth = INF;
  std::queue<int> q;
  q.push(src);
  while( not q.empty() ) {
    int id = q.front(); q.pop();
    if( id == dest ) limitdepth = depth[id];
    if( depth[id] > limitdepth ) break;
    for(std::tuple<int, int> I : graph[id]) {
      int next, limit;
      std::tie(next, limit) = I;
      if( limit - flow[id][next] <= 0 ) continue;
      if( depth[next] != INF ) continue;
      depth[next] = depth[id] + 1;
      q.push(next);
    }
  }
  // printf("update depth\n");
  for(int i = 0; i < V; ++i) {
    // printf("depth[%d] : %d\n", i, depth[i]);
  }
}

// try flow once
// return := one flow
int prev_count[maxV];
void initdfs() {
  for(int i = 0; i < V; ++i) {
    prev_count[i] = 0;
  }
}
int dfs(int id, int fm) {
  if( id == dest ) return fm;
  if( fm == 0 ) return 0;
  if( finished[id] ) return 0;
  // for(std::tuple<int, int> I : graph[id]) {
  for(int i = prev_count[id]; i < (int)graph[id].size(); ++i) {
    prev_count[id] = std::max(prev_count[id], i-1);
    std::tuple<int, int> I = graph[id][i];
    int next, limit;
    std::tie(next, limit) = I;
    if( not ( depth[id] < depth[next] ) ) continue;
    int ft = dfs(next, std::min(fm, limit - flow[id][next]));
    if( ft <= 0 ) continue;
    flow[id][next] += ft;
    flow[next][id] -= ft;
    return ft;
  }
  finished[id] = true;
  return 0;
}

int Dinic() {
  int total = 0;
  for(;;) {
    bfs();
    for(int i = 0; i < V; ++i) finished[i] = false;
    int f = 0;
    initdfs();
    for(;;) {
      int ft = dfs(src, INF);
      if( ft == 0 ) break;
      f += ft;
    }
    if( f == 0 ) break;
    total += f;
  }
  return total;
}

int main() {
  scanf("%d %d", &h, &w);
  for(int i = 0; i < h; ++i) {
    scanf("%s", field[i]);
  }
  setupDinic();

  int num_pair = Dinic();
  // printf(" %d\n", num_pair);
  int ww = 0, bb = 0;
  for(int r = 0; r < h; ++r) {
    for(int c = 0; c < w; ++c) {
      if( field[r][c] == 'w' ) ww += 1;
      if( field[r][c] == 'b' ) bb += 1;
    }
  }
  ww -= num_pair;
  bb -= num_pair;
        
  printf("%d\n", 100 * num_pair + 10 * std::min(ww, bb) + std::abs(ww - bb));
  
  return 0;
}

0