結果

問題 No.421 しろくろチョコレート
ユーザー simansiman
提出日時 2022-03-15 04:14:43
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 2,981 bytes
コンパイル時間 5,005 ms
コンパイル使用メモリ 133,568 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 15:43:57
合計ジャッジ時間 3,189 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 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 2 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 1 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 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 1 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 4 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 1 ms
4,348 KB
testcase_42 AC 1 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 3 ms
4,348 KB
testcase_45 AC 1 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 3 ms
4,348 KB
testcase_48 AC 3 ms
4,348 KB
testcase_49 AC 1 ms
4,348 KB
testcase_50 AC 1 ms
4,348 KB
testcase_51 AC 3 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 2 ms
4,348 KB
testcase_58 AC 2 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 4 ms
4,348 KB
testcase_61 AC 4 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 1 ms
4,348 KB
testcase_64 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int INF = INT_MAX;

struct Edge {
  int to;
  int cap;
  int rev;

  Edge(int to = -1, int cap = -1, int rev = -1) {
    this->to = to;
    this->cap = cap;
    this->rev = rev;
  }
};

const int MAX_N = 100;
const int MAX_V = MAX_N * MAX_N + 10;
int level[MAX_V];
int iter[MAX_V];
vector <Edge> G[MAX_V];

void add_edge(int from, int to, int cap) {
  G[from].push_back(Edge(to, cap, G[to].size()));
  G[to].push_back(Edge(from, 0, G[from].size() - 1));
}

void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);

  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (int i = 0; i < (int) G[v].size(); ++i) {
      Edge *e = &G[v][i];

      if (e->cap > 0 && level[e->to] < 0) {
        level[e->to] = level[v] + 1;
        que.push(e->to);
      }
    }
  }
}

int dfs(int v, int t, int f) {
  if (v == t) return f;

  for (int &i = iter[v]; i < (int) G[v].size(); ++i) {
    Edge *e = &G[v][i];

    if (e->cap > 0 && level[v] < level[e->to]) {
      int d = dfs(e->to, t, min(f, e->cap));
      if (d > 0) {
        e->cap -= d;
        G[e->to][e->rev].cap += d;
        return d;
      }
    }
  }

  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;

  for (;;) {
    bfs(s);
    if (level[t] < 0) return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int DY[4] = {-1, 0, 1, 0};
int DX[4] = {0, 1, 0, -1};

int main() {
  int N, M;
  cin >> N >> M;

  string grid[N];
  int counter[2] = {0, 0};

  for (int i = 0; i < N; ++i) {
    cin >> grid[i];

    for (int j = 0; j < M; ++j) {
      if (grid[i][j] == 'b') {
        counter[0]++;
      } else if (grid[i][j] == 'w') {
        counter[1]++;
      }
    }
  }

  int s = MAX_V - 2;
  int t = MAX_V - 1;

  for (int y = 0; y < N; ++y) {
    for (int x = 0; x < M; ++x) {
      int u = y * M + x;
      char ch = grid[y][x];
      if (ch == '.') continue;

      if (ch == 'b') {
        add_edge(s, u, 1);
      } else {
        add_edge(u, t, 1);
      }

      if (ch == 'b') {
        for (int i = 0; i < 4; ++i) {
          int ny = y + DY[i];
          int nx = x + DX[i];

          if (ny < 0 || nx < 0 || N <= ny || M <= nx) continue;
          if (grid[ny][nx] == '.') continue;
          if (ch == grid[ny][nx]) continue;

          int v = ny * M + nx;

          add_edge(u, v, 1);
        }
      }
    }
  }

  int mc = max_flow(s, t);
  fprintf(stderr, "mc: %d\n", mc);
  int ans = 100 * mc;
  counter[0] -= mc;
  counter[1] -= mc;
  int pc = min(counter[0], counter[1]);
  ans += 10 * pc;
  counter[0] -= pc;
  counter[1] -= pc;
  ans += counter[0] + counter[1];

  cout << ans << endl;

  return 0;
}

0