結果

問題 No.459 C-VS for yukicoder
ユーザー ferinferin
提出日時 2019-10-21 03:28:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 5,794 bytes
コンパイル時間 3,730 ms
コンパイル使用メモリ 187,964 KB
実行使用メモリ 32,524 KB
最終ジャッジ日時 2023-09-15 14:54:30
合計ジャッジ時間 5,813 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,500 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 68 ms
32,524 KB
testcase_22 AC 46 ms
28,872 KB
testcase_23 AC 49 ms
29,660 KB
testcase_24 AC 24 ms
13,668 KB
testcase_25 AC 12 ms
7,972 KB
testcase_26 AC 7 ms
6,180 KB
testcase_27 AC 7 ms
6,092 KB
testcase_28 AC 12 ms
8,176 KB
testcase_29 AC 16 ms
12,036 KB
testcase_30 AC 7 ms
6,480 KB
testcase_31 AC 13 ms
8,156 KB
testcase_32 AC 3 ms
4,408 KB
testcase_33 AC 18 ms
9,388 KB
testcase_34 AC 19 ms
10,224 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 14 ms
9,632 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 17 ms
9,496 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 12 ms
7,548 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 9 ms
7,000 KB
testcase_43 AC 16 ms
9,224 KB
testcase_44 AC 5 ms
4,704 KB
testcase_45 AC 4 ms
4,744 KB
testcase_46 AC 4 ms
4,412 KB
testcase_47 AC 3 ms
4,380 KB
testcase_48 AC 16 ms
9,572 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 17 ms
8,852 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 8 ms
5,940 KB
testcase_53 AC 4 ms
4,376 KB
testcase_54 AC 19 ms
9,548 KB
testcase_55 AC 5 ms
4,992 KB
testcase_56 AC 11 ms
7,480 KB
testcase_57 AC 3 ms
4,380 KB
testcase_58 AC 18 ms
10,192 KB
testcase_59 AC 5 ms
5,072 KB
testcase_60 AC 8 ms
5,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

template<class T>
struct dinic {
    struct edge{
        int to;
        T cap;
        int rev;
        bool isrev;
    };

    vector<vector<edge>> G;
    vector<int> level, iter;

    void bfs(int s) {
        level.assign(G.size(), -1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while(que.size()) {
            int v = que.front(); que.pop();
            for(auto i: G[v]) {
                if(i.cap > 0 && level[i.to] < 0) {
                    level[i.to] = level[v] + 1;
                    que.push(i.to);
                }
            }
        }
    }

    T dfs(int v, const int t, T f) {
        if(v == t) return f;
        for(int &i = iter[v]; i<(ll)G[v].size(); ++i) {
            edge &e = G[v][i];
            if(e.cap > 0 && level[v] < level[e.to]) {
                T 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;
    }

    dinic() {}
    dinic(int n) : G(n), level(n), iter(n) {}

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

    // sからtへ流量fを流す
    T maxflow(int s, int t, T f = 1LL<<30) {
        T flow = 0;
        while(1) {
            bfs(s);
            if(level[t] < 0) return flow;
            iter.assign(G.size(), 0);
            T tmp;
            while((tmp = dfs(s, t, f)) > 0) flow += tmp;
        }
    }

    // sからtへ1流す
    T flow(int s, int t) {
        bfs(s);
        if(level[t] < 0) return 0;
        iter.assign(G.size(), 0);
        return dfs(s, t, 1);
    }
    // 始点,終点tのフローで辺e=(from,to)の容量を1増やしたときの最大流の変化
    // 並列辺はたぶんバグる
    T add(int from, int to, int s, int t) {
        for(auto &e: G[from]) {
            if(e.to == to && !e.isrev) {
                e.cap++;
                break;
            }
        }
        return flow(s, t);
    }
    // 始点s,終点tのフローで辺e=(from,to)の容量を1減らしたときの最大流の変化
    // 並列辺はたぶんバグる
    T sub(int from, int to, int s, int t) {
        for(auto &e: G[from]) {
            if(e.to == to && !e.isrev) {
                T diff = 0;
                // 辺(from,to)で容量いっぱいに流れている
                if(e.cap == 0) {
                    // 残余グラフでfrom→toのパスがない
                    if(flow(from, to) == 0) {
                        flow(t, to);
                        flow(from, s);
                        diff = -1;
                    }
                    G[e.to][e.rev].cap--;
                } else {
                    e.cap--;
                }
                return diff;
            }
        }
        assert(false); // 存在しない辺を減らそうとした
    }

    friend ostream &operator <<(ostream& out, const dinic& a){
        out << endl;
        for(int i = 0; i < (int)a.G.size(); i++) {
            for(auto &e : a.G[i]) {
                if(e.isrev) continue;
                auto &rev_e = a.G[e.to][e.rev];
                out << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
            }
        }
        return out;
    }
};

template<class F>
struct dinic_with_lowerlimit {
    int S, T;
    F sum_lb;
    dinic<F> flow;

    dinic_with_lowerlimit() {}
    dinic_with_lowerlimit(int n) : S(n), T(n+1), sum_lb(0), flow(n+2) {}

    void add_edge(int from, int to, F ub) {
        flow.add_edge(from, to, ub);
    }
    void add_edge(int from, int to, F lb, F ub) {
        flow.add_edge(from, to, ub-lb);
        flow.add_edge(S, to, lb);
        flow.add_edge(from, T, lb);
        sum_lb += lb;
    }

    // sからtへ最小流量を満たすフローが存在しなければ-1
    F maxflow(int s, int t) {
        F a = flow.maxflow(S, T);
        F b = flow.maxflow(s, T);
        F c = flow.maxflow(S, t);
        F d = flow.maxflow(s, t);
        return (a+b==sum_lb && a+c==sum_lb) ? b+d : -1;
    }
};

int main(void) {
    ll h, w, n;
    cin >> h >> w >> n;
    vector<string> v(h);
    REP(i, h) cin >> v[i];
    vector<ll> x(n);
    REP(i, n) cin >> x[i];

    vector<ll> cnt(w);
    REP(i, h) REP(j, w) if(v[i][j]=='#') cnt[j]++;

    dinic_with_lowerlimit<ll> flow(n+w+2);
    ll s = n+w, t = s+1;

    REP(i, n) flow.add_edge(s, i, 1, 9);
    REP(i, n) {
        flow.add_edge(i, n+x[i], 3);
        flow.add_edge(i, n+x[i]+1, 3);
        flow.add_edge(i, n+x[i]+2, 3);
    }
    REP(i, w) flow.add_edge(n+i, t, cnt[i]);
    flow.maxflow(s, t);

    vector<vector<string>> ans(n, vector<string>(3, string(3, '.')));
    auto G = flow.flow.G;
    REP(i, n) {
        for(auto e: G[i]) {
            if(n <= e.to && e.to < n+w && !e.isrev) {
                ll c = e.to - n - x[i];
                ll num = G[e.to][e.rev].cap;
                REP(j, num) ans[i][j][c] = '#';
            }
        }
    }

    REP(i, n) REP(j, 3) cout << ans[i][j] << '\n';
    cout << flush;

    return 0;
}
0