結果

問題 No.421 しろくろチョコレート
ユーザー alpha_virginisalpha_virginis
提出日時 2016-09-10 01:08:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,023 bytes
コンパイル時間 1,803 ms
コンパイル使用メモリ 177,444 KB
実行使用メモリ 43,008 KB
最終ジャッジ日時 2024-11-16 19:11:57
合計ジャッジ時間 4,156 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 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]) {
    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;
    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