結果

問題 No.459 C-VS for yukicoder
ユーザー maimai
提出日時 2019-03-05 23:54:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 14,678 bytes
コンパイル時間 3,684 ms
コンパイル使用メモリ 241,428 KB
実行使用メモリ 19,424 KB
最終ジャッジ日時 2023-09-05 18:59:48
合計ジャッジ時間 7,791 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,504 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 91 ms
19,424 KB
testcase_22 AC 66 ms
17,492 KB
testcase_23 AC 70 ms
17,900 KB
testcase_24 AC 29 ms
9,480 KB
testcase_25 AC 15 ms
6,404 KB
testcase_26 AC 11 ms
4,868 KB
testcase_27 AC 10 ms
5,052 KB
testcase_28 AC 15 ms
6,392 KB
testcase_29 AC 23 ms
7,996 KB
testcase_30 AC 10 ms
4,984 KB
testcase_31 AC 17 ms
6,164 KB
testcase_32 AC 4 ms
4,380 KB
testcase_33 AC 22 ms
7,080 KB
testcase_34 AC 24 ms
7,552 KB
testcase_35 AC 4 ms
4,388 KB
testcase_36 AC 20 ms
6,872 KB
testcase_37 AC 4 ms
4,380 KB
testcase_38 AC 21 ms
6,928 KB
testcase_39 AC 4 ms
4,380 KB
testcase_40 AC 16 ms
5,832 KB
testcase_41 AC 4 ms
4,380 KB
testcase_42 AC 13 ms
5,404 KB
testcase_43 AC 21 ms
6,700 KB
testcase_44 AC 6 ms
4,376 KB
testcase_45 AC 5 ms
4,396 KB
testcase_46 AC 4 ms
4,376 KB
testcase_47 AC 4 ms
4,500 KB
testcase_48 AC 21 ms
6,772 KB
testcase_49 AC 4 ms
4,380 KB
testcase_50 AC 20 ms
6,644 KB
testcase_51 AC 4 ms
4,376 KB
testcase_52 AC 10 ms
5,000 KB
testcase_53 AC 4 ms
4,376 KB
testcase_54 AC 22 ms
7,004 KB
testcase_55 AC 6 ms
4,376 KB
testcase_56 AC 14 ms
5,416 KB
testcase_57 AC 4 ms
4,536 KB
testcase_58 AC 23 ms
7,152 KB
testcase_59 AC 6 ms
4,380 KB
testcase_60 AC 9 ms
4,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"

using namespace std;
using ll = long long int;

#define debugos cout
#define debug(v) {printf("L%d %s > ",__LINE__,#v);debugos<<(v)<<endl;}
#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto edgeidx:(v)){debugos<<edgeidx<<" ";}debugos<<endl;}
#define debuga(m,f) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(f);x++){debugos<<(m)[x]<<" ";}debugos<<endl;}
#define debugaa(m,h,f) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(f);x++){debugos<<(m)[y][x]<<" ";}debugos<<endl;}}
#define ALL(v) (v).begin(),(v).end()
#define repeat(cnt,l) for(remove_reference<remove_const<decltype(l)>::type>::type cnt=0;(cnt)<(l);++(cnt))
#define rrepeat(cnt,l) for(auto cnt=(l)-1;0<=(cnt);--(cnt))
#define iterate(cnt,b,edgeidx) for(auto cnt=(b);(cnt)!=(edgeidx);++(cnt))
#define diterate(cnt,b,edgeidx) for(auto cnt=(b);(cnt)!=(edgeidx);--(cnt))
const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L;
inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } }
template<typename T1, typename T2> inline ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; }
template<typename Vec> inline ostream& _ostream_vecprint(ostream& os, const Vec& a) {
    os << '['; for (const auto& e : a) os << ' ' << e << ' '; os << ']'; return os;
}
template<typename T> inline ostream& operator<<(ostream& o, const vector<T>& v) { return _ostream_vecprint(o, v); }
template<typename T, size_t S> inline ostream& operator<<(ostream& o, const array<T, S>& v) { return _ostream_vecprint(o, v); }
template<typename T> inline T& maxset(T& to, const T& val) { return to = max(to, val); }
template<typename T> inline T& minset(T& to, const T& val) { return to = min(to, val); }
void bye(string s, int code = 0) { cout << s << endl; exit(code); }
mt19937_64 randdev(8901016);
template<typename T> inline T rand(T l, T h) { return uniform_int_distribution<T>(l, h)(randdev); }
template<> inline double rand<double>(double l, double h) { return uniform_real_distribution<double>(l, h)(randdev); }
template<> inline float rand<float>(float l, float h) { return uniform_real_distribution<float>(l, h)(randdev); }

#if defined(_WIN32) || defined(_WIN64)
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#elif defined(__GNUC__)
#else
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif
namespace {
#define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)
    class MaiScanner {
    public:
        template<typename T> void input_integer(T& var) noexcept {
            var = 0; T sign = 1;
            int cc = getchar_unlocked();
            for (; cc < '0' || '9' < cc; cc = getchar_unlocked())
                if (cc == '-') sign = -1;
            for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked())
                var = (var << 3) + (var << 1) + cc - '0';
            var = var * sign;
        }
        inline int c() noexcept { return getchar_unlocked(); }
        inline MaiScanner& operator>>(int& var) noexcept { input_integer<int>(var); return *this; }
        inline MaiScanner& operator>>(long long& var) noexcept { input_integer<long long>(var); return *this; }
        inline MaiScanner& operator>>(string& var) {
            int cc = getchar_unlocked();
            for (; !isvisiblechar(cc); cc = getchar_unlocked());
            for (; isvisiblechar(cc); cc = getchar_unlocked())
                var.push_back(cc);
            return *this;
        }
        template<typename IT> void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; }
    };
    class MaiPrinter {
    public:
        template<typename T>
        void output_integer(T var) noexcept {
            if (var == 0) { putchar_unlocked('0'); return; }
            if (var < 0)
                putchar_unlocked('-'),
                var = -var;
            char stack[32]; int stack_p = 0;
            while (var)
                stack[stack_p++] = '0' + (var % 10),
                var /= 10;
            while (stack_p)
                putchar_unlocked(stack[--stack_p]);
        }
        inline MaiPrinter& operator<<(char c) noexcept { putchar_unlocked(c); return *this; }
        inline MaiPrinter& operator<<(int var) noexcept { output_integer<int>(var); return *this; }
        inline MaiPrinter& operator<<(long long var) noexcept { output_integer<long long>(var); return *this; }
        inline MaiPrinter& operator<<(char* str_p) noexcept { while (*str_p) putchar_unlocked(*(str_p++)); return *this; }
        inline MaiPrinter& operator<<(const string& str) {
            const char* p = str.c_str();
            const char* l = p + str.size();
            while (p < l) putchar_unlocked(*p++);
            return *this;
        }
        template<typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; }
    };
}
MaiScanner scanner;
MaiPrinter printer;



class DGraphF {
public:
    typedef int cap_t;
    size_t n_;
    struct Arc {
        int from, to;
        // 残量
        cap_t left;
        // 容量
        cap_t cap;

        Arc(int from = 0, int to = 0, cap_t w = 1) :from(from), to(to), left(w), cap(w) {}
        inline bool operator<(const Arc& a) const { return (left != a.left) ? left < a.left : (left < a.left) | (cap < a.cap) | (from < a.from) | (to < a.to); }
        inline bool operator==(const Arc& a) const { return (from == a.from) && (to == a.to) && (left == a.left) && (cap == a.cap); }
    };
    vector<vector<int>> vertex_to;
    vector<vector<int>> vertex_from;
    vector<Arc> edges;

    DGraphF(int n = 1) :n_(n), vertex_to(n), vertex_from(n) { }

    void connect(int from, int to, cap_t left) {
        vertex_to[(size_t)from].push_back((int)edges.size()); // toto
        vertex_from[(size_t)to].push_back((int)edges.size()); // fromfrom
        edges.emplace_back(from, to, left);
    }

    inline size_t size() const { return n_; }
};


void dinic(DGraphF &graph, vector<DGraphF::cap_t>& result, int i_source, int i_sink) {
    assert(i_source != i_sink);

    result.resize(graph.n_);
    vector<int> dist(graph.n_);
    vector<int8_t> visited(graph.n_);

    auto _dfs = [&](auto&& _dfs, int u, int i_sink, DGraphF::cap_t mini) -> DGraphF::cap_t {
        // DAG
        // TODO: 経路再利用
        if (i_sink == u) return mini;
        if (visited[u]) return -1;
        visited[u] = true;

        DGraphF::cap_t sumw = 0;
        bool term = true;
        for (int edgeidx : graph.vertex_to[u]) {
            auto& edge = graph.edges[edgeidx];
            if (edge.left > 0 && dist[u] > dist[edge.to]) {
                DGraphF::cap_t f = (mini < 0) ? edge.left : min(edge.left, mini);

                f = _dfs(_dfs, edge.to, i_sink, f);
                if (f == -1) continue;
                edge.left -= f;
                result[edge.to] += f;

                sumw += f;
                mini -= f;
                term = false;
                visited[u] = false; // TODO: 末尾では?
                if (mini == 0) return sumw;
            }
        }
        for (int edgeidx : graph.vertex_from[u]) {
            auto& edge = graph.edges[edgeidx];
            if (edge.cap > edge.left && dist[u] > dist[edge.from]) {
                DGraphF::cap_t f = (mini < 0) ? (edge.cap - edge.left) : min(edge.cap - edge.left, mini);

                f = _dfs(_dfs, edge.from, i_sink, f);
                if (f == -1) continue;
                edge.left += f;
                result[edge.to] -= f;

                sumw += f;
                mini -= f;
                term = false;
                visited[u] = false;
                if (mini == 0) return sumw;
            }
        }
        return term ? -1 : sumw;
    };

    queue<int> que;
    for (int distbegin = 0; ; distbegin += (int)graph.n_) {
        // sinkからsourceへの距離を計算.
        que.emplace(i_sink);
        dist[i_sink] = distbegin + 1;
        while (!que.empty()) {
            int v = que.front(); que.pop();
            for (int edgeidx : graph.vertex_from[v]) {
                const auto edge = graph.edges[edgeidx];
                if (0 < edge.left && dist[edge.from] <= distbegin) {
                    dist[edge.from] = dist[v] + 1;
                    que.push(edge.from);
                }
            }
            for (int edgeidx : graph.vertex_to[v]) {
                const auto edge = graph.edges[edgeidx];
                if (edge.left < edge.cap && dist[edge.to] <= distbegin) {
                    dist[edge.to] = dist[v] + 1;
                    que.push(edge.to);
                }
            }
        }
        fill(visited.begin(), visited.end(), false);

        if (dist[i_source] <= distbegin)
            break;
        else
            result[i_source] += _dfs(_dfs, i_source, i_sink, -1);
    }
}



// ## 最小流量制限付き最大フロー
// + http://snuke.hatenablog.com/entry/2016/07/10/043918
// + http://yukicoder.me/submissions/137248
// + http://yukicoder.me/submissions/143696
// 
// #### 解説
// 最小流量制限付き最大フローは,普通の最大フローに置き換えることができる.
// 
// 面倒なので,最小流l,最大流hで頂点uから頂点vへ流れる有向辺を(u,v)[l,h]と表記する.
// 
// + s→tな最大最小流量制限付きフローG=(V,E)を考える.最大流量制限付きフローG'を作りたい.
// + 新たに頂点S,Tを作る.
// + (u,v)[c,c+d]がGに存在するとき,G'に(u,v)[0,d],(u,T)[0,c],(S,v)[0,c]を与える.
// + G'に多重辺が出来ることがある.
// + S→T,S→t,s→T,s→tの順に最大流を求める.S,Tに隣接する辺に優先して流すため.
// + S,Tに隣接する辺が全てemptyになっていれば,条件を満たすフローが存在
// + 流量は(u,v)+(u,T)
// 
// 事前に全体の流量が把握出来るならば, #137248のように,S→s,t→Tの辺を作ってS→Tを流せばよい


class FlowMinMax {
public:
    DGraphF graph;
    const int v_source; // vertex of new source
    FlowMinMax(int n) :graph(n + 2), v_source(n) {}

private:
    bool _solve_dinic_edge(map<pair<int, int>, int>& result_edge, int i_source, int i_sink) {

        vector<int> resflow(graph.size(), 0);

        dinic(graph, resflow, v_source, v_source + 1);
        dinic(graph, resflow, v_source, i_sink);
        dinic(graph, resflow, i_source, v_source + 1);
        dinic(graph, resflow, i_source, i_sink);

        for (int e : graph.vertex_from[v_source + 1]) {
            const DGraphF::Arc& a = graph.edges[e];
            if (0 < a.left) return false;
        }
        int flow;
        for (int u = 0; u < graph.size() - 2; u++) {
            for (int ei : graph.vertex_to[u]) { // TODO:最適化の余地あり(らしい)
                const DGraphF::Arc& a = graph.edges[ei]; // u -> v
                if (a.to >= graph.size() - 2) {
                    if (0 < a.left) return false;
                    continue;
                }

                const DGraphF::Arc& c = graph.edges[ei + 1]; // S -> v
                if (a.to != c.to) {
                    flow = a.cap - a.left;
                }
                else {
                    if (0 < c.left) return false;
                    flow = c.cap + a.cap - c.left - a.left;
                }
                if (0 < flow)
                    result_edge[make_pair(u, a.to)] += flow;
            }
        }
        return true;
    }

public:

    void connect(int from, int to, int w_min, int w_max) {

        if (w_max == w_min) {
            graph.connect(v_source, to, w_min);
            graph.connect(from, v_source + 1, w_min);
        }
        else if (w_min == 0) {
            graph.connect(from, to, w_max - w_min);
        }
        else {
            graph.connect(from, v_source + 1, w_min);
            graph.connect(from, to, w_max - w_min);
            graph.connect(v_source, to, w_min);
        }
    }

    inline bool solve_dinic_edge(map<pair<int, int>, int>& result_edge, int i_source, int i_sink) {
        return _solve_dinic_edge(result_edge, i_source, i_sink);
    }

};








// https://yukicoder.me/submissions/172443

int width, height;
int m, n;

int field[10010];
int commands[30010];

int main() {
    int i, j, k;
    int x, y, a, b;

    cin >> height >> width >> n;
    cin.ignore();

    int nblocks = 0;
    // X座標にブロックがいくつ積まれているか、を記録する。
    // stringを保持する必要はない。
    for (y = 0; y < height; y++) {
        string s;
        cin >> s;
        for (x = 0; x < width; x++) {
            field[x] += s[x] == '#';
        }
    }
    for (x = 0; x < width; x++) { nblocks += field[x]; }

    for (i = 0; i < n; i++) {
        scanf("%d", commands + i);
    }


    //          A   _    B    _   C
    //             | | ----> | | ----> [sink]
    // [source] -> | |       | |
    //             | |       | |
    //             |_pack    |_field
    // 
    // A : [1,9] (packは[1,9]個のブロックを持つ)
    // B : [0,3] (packは3x3の容量を持つ)
    // C : [#,#] (x列には#個のブロックが積み上がっている)

    FlowMinMax flow(1 + n + width + 1);

    const int i_source = 0;
    const int i_sink = 1;

    for (i = 0; i < n; i++) {
        // A edge
        flow.connect(i_source, 2 + i, 1, 9);
        int left = commands[i];
        for (j = 0; j < 3; j++) {
            // B edge
            flow.connect(2 + i, 2 + n + left + j, 0, 3);
        }
    }
    for (x = 0; x < width; x++) {
        // C edge
        flow.connect(2 + n + x, i_sink, field[x], field[x]);
    }

    //for (Flow::Arrow& ar : flow.flow.arrow){
    //    if (ar.w_max == 0) continue;
    //    printf("%d -> %d\n",ar.from,ar.to);
    //}

    map<pair<int, int>, int> fl;
    if (!flow.solve_dinic_edge(fl, i_source, i_sink)) {
        abort();
        cout << "warn" << endl;
    }

    //debugv(nagare);
    int hako[3];
    for (i = 0; i < n; ++i) {
        for (j = 0; j < 3; ++j) {
            hako[j] = fl[make_pair(2 + i, 2 + n + commands[i] + j)];
        }
        for (y = 3; 0 < y; --y) {
            for (x = 0; x < 3; ++x) {
                if (y <= hako[x]) {
                    putchar_unlocked('#');
                }
                else {
                    putchar_unlocked('.');
                }
            }
            putchar_unlocked('\n');
        }
    }


    return 0;
}
0