結果

問題 No.421 しろくろチョコレート
ユーザー どららどらら
提出日時 2016-09-10 00:09:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 2,990 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 102,264 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:47:40
合計ジャッジ時間 2,387 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 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 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 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 3 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 1 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 3 ms
4,348 KB
testcase_33 AC 3 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 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 2 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 1 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 3 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 3 ms
4,348 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 1 ms
4,348 KB
testcase_54 AC 1 ms
4,348 KB
testcase_55 AC 1 ms
4,348 KB
testcase_56 AC 1 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 1 ms
4,348 KB
testcase_63 AC 1 ms
4,348 KB
testcase_64 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <stack>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;

int N, M;
string field[55];

#define MAX_V 2505
int V;
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V];

void init_graph(){
  for(int i=0; i<MAX_V; i++){
    G[i].clear();
    match[i] = -1;
    used[i] = false;
  }
}
void add_edge(int u, int v){
  G[u].push_back(v);
  G[v].push_back(u);
}
bool dfs(int v){
  used[v] = true;
  for(int i=0; i<G[v].size(); i++){
    int u = G[v][i], w = match[u];
    if(w<0||!used[w]&&dfs(w)){
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}
int bipartite_matching(){
  int res = 0;
  rep(i,MAX_V) match[i] = -1;
  for(int v=0; v<V; v++){
    if(match[v] < 0){
      rep(i,MAX_V) used[i] = false;
      if(dfs(v)){
        res++;
      }
    }
  }
  return res;
}

int vx[4] = {0,0,1,-1};
int vy[4] = {1,-1,0,0};

int main(){
  ios::sync_with_stdio(false);

  int b_cnt = 0;
  int w_cnt = 0;
  map< pair<int,int>, int> b_memo, w_memo;
  map<int, pair<int,int> > b_number, w_number;
  init_graph();

  cin >> N >> M;
  rep(i,N){
    cin >> field[i];
    rep(j,M){
      if(field[i][j] == 'b'){
        b_number[b_cnt] = make_pair(i,j);
        b_memo[make_pair(i,j)] = b_cnt++;
      }
      if(field[i][j] == 'w'){
        w_number[w_cnt] = make_pair(i,j);
        w_memo[make_pair(i,j)] = w_cnt++;
      }
    }
  }
  
  V = b_cnt + w_cnt;
  
  rep(i,N){
    rep(j,M){
      if(field[i][j] == 'b'){
        rep(k,4){
          int nx = j + vx[k];
          int ny = i + vy[k];
          if(0<=nx && nx<M && 0<=ny && ny<N){
            if(field[ny][nx] == 'w'){
              int bn = b_memo[make_pair(i,j)];
              int wn = w_memo[make_pair(ny,nx)];

              add_edge(bn, b_cnt+wn);
            }
          }
        }
      }
    }
  }

  ll ret = 0;
  
  int bm = bipartite_matching();
  ret = bm * 100;
  rep(i,V){
    if(i<b_cnt && match[i]>=0){
      pair<int,int> p = w_number[match[i]-b_cnt];
      pair<int,int> q = b_number[i];
      field[p.first][p.second] = 'x';
      field[q.first][q.second] = 'x';
    }
    if(i>=b_cnt && match[i]>=0){
      pair<int,int> p = b_number[match[i]];
      pair<int,int> q = w_number[i-b_cnt];
      field[p.first][p.second] = 'x';
      field[q.first][q.second] = 'x';
    }
  }

  int bb = 0;
  int ww = 0;
  rep(i,N){
    rep(j,M){
      if(field[i][j] == 'w') ww++;
      if(field[i][j] == 'b') bb++;
    }
  }

  int mn = min(bb,ww);
  ret += mn * 10;

  bb-=mn;
  ww-=mn;
  ret += bb + ww;

  cout << ret << endl;
  
  return 0;
}

0