結果

問題 No.421 しろくろチョコレート
ユーザー parukiparuki
提出日時 2016-09-09 22:42:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 3,356 bytes
コンパイル時間 1,877 ms
コンパイル使用メモリ 181,272 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:45:23
合計ジャッジ時間 3,455 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 4 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 4 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 3 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 10 ms
4,348 KB
testcase_18 AC 14 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 6 ms
4,348 KB
testcase_21 AC 5 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 2 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 3 ms
4,348 KB
testcase_29 AC 4 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 6 ms
4,348 KB
testcase_32 AC 3 ms
4,348 KB
testcase_33 AC 4 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 14 ms
4,348 KB
testcase_38 AC 15 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 3 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 4 ms
4,348 KB
testcase_48 AC 4 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 7 ms
4,348 KB
testcase_52 AC 3 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 1 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 4 ms
4,348 KB
testcase_59 AC 3 ms
4,348 KB
testcase_60 AC 16 ms
4,348 KB
testcase_61 AC 7 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

struct FlowEdge{
    int to, cap, rev;
    FlowEdge(int to_, int cap_, int rev_):to(to_),cap(cap_),rev(rev_){ }
};
class FordFulkerson{
public:
    FordFulkerson(){}

    FordFulkerson(int n) : G(n), used(n){}

    void add(int from, int to, int cap){
        G[from].emplace_back(to, cap, (int)G[to].size() );
        G[to].emplace_back(from, 0, (int)G[from].size() - 1 );
    }

    int get(int s, int t){
        int f = 1, res = 0;
        while (f){
            fill(begin(used), end(used), false);
            f = dfs(s, t, INT_MAX);
            res += f;
        }
        return res;
    }
private:

    vector<vector<FlowEdge>> G;
    vector<bool> used;

    int dfs(int v, int t, int f){
        if (v == t) return f;
        used[v] = 1;
        for (auto &e : G[v]){
            if (!used[e.to] && e.cap > 0){
                int d = dfs(e.to, t, min(e.cap, f));
                if (d > 0){
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
};

int maxBipartiteMatching(int n, int m, const vector<int> &u, const vector<int> &v){
    const int source = 0, sink = 1, offN = 2, offM = n + 2;
    const int V = n + m + 2, cap = 1;
    FordFulkerson maxFlow(V);
    for(int i = 0; i < n; ++i){
        maxFlow.add(source, i + offN, cap);
    }
    for(int i = 0; i < m; ++i){
        maxFlow.add(i + offM, sink, cap);
    }
    for(int i = 0; i < (int)u.size(); ++i){
        maxFlow.add(u[i] + offN, v[i] + offM, cap);
    }
    return maxFlow.get(source, sink);
}

string nstr(){
    static const int MAX_LEN = 101;
    static char res_[MAX_LEN];
    scanf("%s",res_);
    return string(res_);
}

int ww[51][51], bb[51][51];

int main(){
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    rep(y, H)S[y] = nstr();

    int wn = 0, bn = 0;
    MEM(ww, -1);
    MEM(bb, -1);
    rep(y, H)rep(x, W){
        if(S[y][x] == 'w')ww[y][x] = wn++;
        else if(S[y][x] == 'b')bb[y][x] = bn++;
    }

    vi us, vs;
    rep(y, H)rep(x, W){
        if(ww[y][x] == -1)continue;
        if(x > 0 && bb[y][x - 1] != -1){
            us.push_back(ww[y][x]);
            vs.push_back(bb[y][x - 1]);
        }
        if(x < W-1 && bb[y][x+1] != -1){
            us.push_back(ww[y][x]);
            vs.push_back(bb[y][x+1]);
        }
        if(y > 0 && bb[y-1][x] != -1){
            us.push_back(ww[y][x]);
            vs.push_back(bb[y-1][x]);
        }
        if(y < H-1 && bb[y+1][x] != -1){
            us.push_back(ww[y][x]);
            vs.push_back(bb[y+1][x]);
        }
    }
    
    int ps = maxBipartiteMatching(wn, bn, us, vs);
    wn -= ps;
    bn -= ps;
    int ans = ps * 100;
    int mi = min(wn, bn);
    ans += mi * 10;
    wn -= mi;
    bn -= mi;
    ans += wn + bn;
    cout << ans << endl;
}
0