結果

問題 No.421 しろくろチョコレート
ユーザー umimelumimel
提出日時 2022-12-30 15:50:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 3,698 bytes
コンパイル時間 2,016 ms
コンパイル使用メモリ 193,380 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-04 05:07:56
合計ジャッジ時間 3,831 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

struct Dinic{
    struct Edge{
        ll to;
        ll cap;
        ll rev;
    };

    vector<vector<Edge>> G;
    vector<ll> level;
    vector<ll> iter;
    ll N;

    Dinic(vector<vector<pll>> &G_){
        N = (ll)G_.size();
        G.resize(N);
        level.resize(N);
        iter.resize(N);

        rep(from, N){
            for(pll p : G_[from]){
                ll to = p.fi;
                ll cap = p.se;
                G[from].pb((Edge){to, cap, (ll)G[to].size()});
                G[to].pb((Edge){from, 0, (ll)G[from].size()-1});
            }
        }
    }

    void bfs(ll s, vector<vector<Edge>> &H){
        rep(i, N) level[i]=-1;

        queue<ll> Q;
        level[s] = 0;
        Q.push(s);
        while(!Q.empty()){
            ll v = Q.front();
            Q.pop();

            for(ll i=0; i<(ll)H[v].size(); i++){
                Edge &e = H[v][i];

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

    ll dfs(ll v, ll t, ll f, vector<vector<Edge>> &H){
        if(v == t) return f;
        for(ll &i=iter[v]; i<(ll)H[v].size(); i++){
            Edge &e = H[v][i];
            if(e.cap > 0 && level[v] < level[e.to]){
                ll d = dfs(e.to, t, min(f, e.cap), H);
                if(d > 0){
                    e.cap -= d;
                    H[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }

        return 0;
    }

    ll max_flow(ll s, ll t){
        vector<vector<Edge>> H(N);
        rep(from, N) for(Edge e : G[from]) H[from].pb(e);

        ll flow = 0;
        for(;;){
            bfs(s, H);
            if(level[t] < 0) return flow;
            rep(i, N) iter[i] = 0;
            ll f;
            while((f = dfs(s, t, INF, H)) > 0){
                flow += f;
            }
        }
    }
};

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

    ll h, w;
    cin >> h >> w;

    vector<vector<char>> a(h, vector<char>(w));
    rep(i, h) rep(j, w) cin >> a[i][j];

    ll n = 0, wn = 0, bn = 0;
    vector<vector<ll>> alloc(h, vector<ll>(w, -1));
    rep(i, h) rep(j, w) if((i+j)%2 == 0 && a[i][j] != '.') alloc[i][j]=n++;
    wn = n;
    rep(i, h) rep(j, w) if((i+j)%2 == 1 && a[i][j] != '.') alloc[i][j]=n++;
    bn = n - wn;

    vector<vector<pll>> G(n+2);
    ll s = n;
    ll t = n+1;

    rep(i, h) rep(j, w){
        if((i+j)%2 == 0 && alloc[i][j] != -1){
            G[s].pb({alloc[i][j], 1});
        }
    }

    ll di[4] = {0, 0, 1, -1};
    ll dj[4] = {1, -1, 0, 0};
    rep(i, h) rep(j, w){
        if((i+j)%2 == 1) continue;
        if(alloc[i][j] == -1) continue;

        rep(k, 4){
            ll next_i = i + di[k];
            ll next_j = j + dj[k];

            if(next_i < 0 || next_j < 0 || h <= next_i || w <= next_j) continue;
            if(alloc[next_i][next_j]==-1) continue;
            G[alloc[i][j]].pb({alloc[next_i][next_j], 1});
        }
    }

    rep(i, h) rep(j, w){
        if((i+j)%2 == 1 && alloc[i][j] != -1){
            G[alloc[i][j]].pb({t, 1});
        }
    }

    Dinic dinic(G);
    ll x, y, z;
    z = dinic.max_flow(s, t);
    y = min(wn-z, bn-z);
    x = n - 2*z - 2*y;
    cout << x + 10*y + 100*z << endl;
}
0