結果

問題 No.421 しろくろチョコレート
ユーザー HaarHaar
提出日時 2019-03-27 23:08:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,191 ms / 2,000 ms
コード長 4,224 bytes
コンパイル時間 2,218 ms
コンパイル使用メモリ 182,692 KB
実行使用メモリ 101,888 KB
最終ジャッジ日時 2024-04-19 13:48:40
合計ジャッジ時間 12,546 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 178 ms
67,328 KB
testcase_02 AC 91 ms
65,280 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 44 ms
32,896 KB
testcase_05 AC 18 ms
8,960 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 58 ms
20,224 KB
testcase_08 AC 102 ms
73,984 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 10 ms
7,424 KB
testcase_11 AC 68 ms
25,216 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 393 ms
94,080 KB
testcase_17 AC 424 ms
101,760 KB
testcase_18 AC 435 ms
101,760 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 184 ms
54,400 KB
testcase_21 AC 311 ms
69,632 KB
testcase_22 AC 77 ms
24,448 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 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 121 ms
101,760 KB
testcase_29 AC 127 ms
101,632 KB
testcase_30 AC 185 ms
101,632 KB
testcase_31 AC 136 ms
101,760 KB
testcase_32 AC 122 ms
101,632 KB
testcase_33 AC 224 ms
101,760 KB
testcase_34 AC 115 ms
101,888 KB
testcase_35 AC 114 ms
101,632 KB
testcase_36 AC 96 ms
38,144 KB
testcase_37 AC 413 ms
79,744 KB
testcase_38 AC 545 ms
101,888 KB
testcase_39 AC 104 ms
54,656 KB
testcase_40 AC 143 ms
24,448 KB
testcase_41 AC 6 ms
6,016 KB
testcase_42 AC 7 ms
6,272 KB
testcase_43 AC 79 ms
47,232 KB
testcase_44 AC 1,191 ms
42,240 KB
testcase_45 AC 39 ms
25,344 KB
testcase_46 AC 27 ms
16,384 KB
testcase_47 AC 171 ms
57,216 KB
testcase_48 AC 131 ms
64,512 KB
testcase_49 AC 98 ms
67,584 KB
testcase_50 AC 43 ms
30,976 KB
testcase_51 AC 204 ms
41,984 KB
testcase_52 AC 59 ms
23,680 KB
testcase_53 AC 28 ms
22,912 KB
testcase_54 AC 126 ms
76,544 KB
testcase_55 AC 7 ms
6,528 KB
testcase_56 AC 6 ms
6,272 KB
testcase_57 AC 26 ms
12,800 KB
testcase_58 AC 184 ms
56,832 KB
testcase_59 AC 28 ms
11,776 KB
testcase_60 AC 656 ms
101,888 KB
testcase_61 AC 465 ms
101,760 KB
testcase_62 AC 2 ms
5,376 KB
testcase_63 AC 2 ms
5,376 KB
testcase_64 AC 92 ms
101,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define FORE(v, a, b) for(int v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(int v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define LLI long long int
#define fst first
#define snd second

#ifdef DEBUG
#include <misc/C++/Debug.cpp>
#else
#define dump(x) ((void)0)
#endif

using namespace std;

#define gcd __gcd
template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;}

template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}
template <typename T, typename U> istream& operator>>(istream &is, pair<T,U> &p){is >> p.first >> p.second; return is;}

template <typename T, typename U> T& chmin(T &a, const U &b){return a = (a<=b?a:b);}
template <typename T, typename U> T& chmax(T &a, const U &b){return a = (a>=b?a:b);}
template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);}

template <typename T, T INF> class Dinic{
private:
  vector<vector<pair<int,T>>> graph;
  int size, s, t;
  vector<vector<T>> cap;
  vector<int> level;
  
  bool buildLevel(){
    fill(ALL(level), 0);
    level[s] = 1;
    deque<int> deq = {s};
    while(!deq.empty()){
      int cur = deq.front(); deq.pop_front();
      REP(i,size)
	if(level[i]==0 && cap[cur][i]>0){
	  level[i] = level[cur] + 1;
	  deq.push_back(i);
	}
    }
    return level[t] != 0;
  }
  void dfs(vector<int> &path, T &flow){
    if(path.empty()) return;
    int cur = path.back();
    if(cur == t){
      T f = INF;
      FOR(i,1,path.size()) f = min(f, cap[path[i-1]][path[i]]);
      FOR(i,1,path.size()){
	cap[path[i-1]][path[i]] -= f;
	cap[path[i]][path[i-1]] += f;
      }
      flow += f;
    }else{
      REP(i,size){
	if(cap[cur][i]>0 && level[i]>level[cur]){
	  path.push_back(i);
	  dfs(path, flow);
	  path.pop_back();
	}
      }
    }
  }
  T augment(){
    T f = 0;
    vector<int> path = {s};
    dfs(path, f);
    return f;
  }
  T loop(){
    T f = 0;
    while(buildLevel()) f += augment();
    return f;
  }

public:
  Dinic(vector<vector<pair<int,T>>> &_graph): graph(_graph), size(graph.size()) {}
  Dinic(int size): graph(size), size(size){}

  void add_edge(int from, int to, const T &cap){
    graph[from].push_back({to, cap});
  }
  
  T max_flow(int _s, int _t){
    cap = vector<vector<T>>(size, vector<T>(size, 0));
    level = vector<int>(size, 0);

    REP(i,size)
      for(auto &p : graph[i]){
	int j = p.first;
	T d = p.second;
	cap[i][j] += d;
      }

    s = _s;
    t = _t;

    return loop();
  }
};

class BipartiteMatching{
public:
  int x, y;
  Dinic<int,INT_MAX> mflow;
  int s, t;
  BipartiteMatching(int x, int y): x(x), y(y), mflow(x+y+2), s(x+y), t(s+1){
    REP(i,x) mflow.add_edge(s,i,1);
    REP(i,y) mflow.add_edge(x+i,t,1);
  }

  void add_edge(int i, int j){
    mflow.add_edge(i,x+j,1);
  }

  int matching(){
    return mflow.max_flow(s,t);
  }
};

const int dir4[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N,M;
  while(cin >> N >> M){
    vector<string> S(N); cin >> S;
    int c = 0;
    REP(i,N) REP(j,M) if(S[i][j] != '.') ++c;

    BipartiteMatching bm(N*M,N*M);

    vector<vector<int>> w(N, vector<int>(M,-1)), b(N, vector<int>(M,-1));
    int cw=0, cb=0;
    REP(i,N) REP(j,M){
      if(S[i][j] == 'w') w[i][j] = cw++;
      if(S[i][j] == 'b') b[i][j] = cb++;
    }

    REP(i,N){
      REP(j,M){
        for(auto &d : dir4){
	  int y=i+d[0], x=j+d[1];
	  if(y<0 or y>=N or x<0 or x>=M) continue;

	  if(S[i][j] == 'w' and S[y][x] == 'b') bm.add_edge(w[i][j], b[y][x]);
	}
      }
    }
    
    int mat = bm.matching();
    int pr = min(cw-mat,cb-mat);
    int ans = mat*100 + pr*10 + (cw-mat-pr) + (cb-mat-pr);

    cout << ans << endl;
  }
  
  return 0;
}
0