結果

問題 No.1678 Coin Trade (Multiple)
ユーザー rniyarniya
提出日時 2024-04-07 17:41:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,215 ms / 5,000 ms
コード長 5,611 bytes
コンパイル時間 1,370 ms
コンパイル使用メモリ 103,948 KB
実行使用メモリ 24,524 KB
最終ジャッジ日時 2024-04-07 17:41:32
合計ジャッジ時間 20,081 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 111 ms
13,384 KB
testcase_04 AC 528 ms
19,904 KB
testcase_05 AC 225 ms
23,608 KB
testcase_06 AC 175 ms
21,864 KB
testcase_07 AC 463 ms
14,900 KB
testcase_08 AC 290 ms
16,184 KB
testcase_09 AC 256 ms
23,648 KB
testcase_10 AC 79 ms
7,896 KB
testcase_11 AC 411 ms
18,988 KB
testcase_12 AC 69 ms
7,988 KB
testcase_13 AC 904 ms
22,552 KB
testcase_14 AC 225 ms
13,024 KB
testcase_15 AC 276 ms
18,452 KB
testcase_16 AC 71 ms
18,452 KB
testcase_17 AC 265 ms
24,000 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
testcase_39 AC 2 ms
6,676 KB
testcase_40 AC 2 ms
6,676 KB
testcase_41 AC 2 ms
6,676 KB
testcase_42 AC 2 ms
6,676 KB
testcase_43 AC 2 ms
6,676 KB
testcase_44 AC 2 ms
6,676 KB
testcase_45 AC 2 ms
6,676 KB
testcase_46 AC 2 ms
6,676 KB
testcase_47 AC 2 ms
6,676 KB
testcase_48 AC 1,186 ms
24,520 KB
testcase_49 AC 1,172 ms
24,516 KB
testcase_50 AC 1,195 ms
24,516 KB
testcase_51 AC 1,215 ms
24,504 KB
testcase_52 AC 1,192 ms
24,524 KB
testcase_53 AC 1,161 ms
24,520 KB
testcase_54 AC 1,184 ms
24,512 KB
testcase_55 AC 1,199 ms
24,516 KB
testcase_56 AC 1,146 ms
24,520 KB
testcase_57 AC 1,182 ms
24,516 KB
testcase_58 AC 685 ms
21,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/no/1678"

#include <iostream>
#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#include <vector>

template <typename Cap, typename Cost> struct PrimalDualonDAG {
    PrimalDualonDAG(int n) : n(n), G(n), h(n), dist(n), prevv(n), preve(n), indeg(n, 0) {}

    int add_edge(int from, int to, Cap cap, Cost cost) {
        assert(0 <= from && from < n);
        assert(0 <= to && to < n);
        assert(0 <= cap);
        // assert(0 <= cost);
        int m = pos.size(), from_id = G[from].size(), to_id = G[to].size();
        pos.emplace_back(from, G[from].size());
        G[from].emplace_back(to, cap, cost, to_id);
        G[to].emplace_back(from, 0, -cost, from_id);
        if (cap > 0) indeg[to]++;
        return m;
    }

    std::tuple<int, int, Cap, Cap, Cost> get_edge(int i) {
        assert(0 <= i && i < (int)pos.size());
        auto e = G[pos[i].first][pos[i].second];
        auto re = G[e.to][e.rev];
        return {pos[i].first, e.to, e.cap + re.cap, re.cap, e.cost};
    }

    std::vector<std::tuple<int, int, Cap, Cap, Cost>> edges() {
        std::vector<std::tuple<int, int, Cap, Cap, Cost>> res;
        for (size_t i = 0; i < pos.size(); i++) res.emplace_back(get_edge(i));
        return res;
    }

    Cost min_cost_flow(int s, int t, Cap flow) {
        auto res = slope(s, t, flow).back();
        return res.first == flow ? res.second : -1;
    }

    std::pair<Cap, Cost> min_cost_max_flow(int s, int t) { return slope(s, t, std::numeric_limits<Cap>::max()).back(); }

    std::vector<std::pair<Cap, Cost>> slope(int s, int t) { return slope(s, t, std::numeric_limits<Cap>::max()); }

  private:
    struct edge {
        int to;
        Cap cap;
        Cost cost;
        int rev;
        edge(int to, Cap cap, Cost cost, int rev) : to(to), cap(cap), cost(cost), rev(rev) {}
    };

    const Cost inf = std::numeric_limits<Cost>::max();
    int n;
    std::vector<std::vector<edge>> G;
    std::vector<std::pair<int, int>> pos;
    std::vector<Cost> h, dist;
    std::vector<int> prevv, preve, indeg, order;

    bool topological_sort() {
        std::queue<int> que;
        for (int i = 0; i < n; i++) {
            if (indeg[i] == 0) {
                que.emplace(i);
            }
        }
        while (!que.empty()) {
            int v = que.front();
            que.pop();
            order.emplace_back(v);
            for (const auto& e : G[v]) {
                if (e.cap > 0 && --indeg[e.to] == 0) {
                    que.emplace(e.to);
                }
            }
        }

        return *std::max_element(indeg.begin(), indeg.end()) == 0;
    }

    void calc_potential(int s) {
        fill(h.begin(), h.end(), inf);
        h[s] = 0;
        for (int& v : order) {
            if (h[v] == inf) continue;
            for (const auto& e : G[v]) {
                if (e.cap > 0) {
                    h[e.to] = std::min(h[e.to], h[v] + e.cost);
                }
            }
        }
    }

    void dijkstra(int s) {
        struct P {
            Cost c;
            int v;
            P(Cost c, int v) : c(c), v(v) {}
            bool operator<(const P& rhs) const { return c > rhs.c; }
        };
        std::priority_queue<P> pq;
        fill(dist.begin(), dist.end(), inf);
        dist[s] = 0;
        pq.emplace(dist[s], s);
        while (!pq.empty()) {
            auto p = pq.top();
            pq.pop();
            int v = p.v;
            if (dist[v] < p.c) continue;
            for (size_t i = 0; i < G[v].size(); i++) {
                auto& e = G[v][i];
                if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
                    dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
                    prevv[e.to] = v;
                    preve[e.to] = i;
                    pq.emplace(dist[e.to], e.to);
                }
            }
        }
    }

    std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {
        assert(0 <= s && s < n);
        assert(0 <= t && t < n);
        assert(s != t);
        assert(topological_sort());
        calc_potential(s);
        Cap flow = 0;
        Cost cost = 0, prev_cost_pre_flow = -1;
        std::vector<std::pair<Cap, Cost>> res;
        res.emplace_back(flow, cost);
        while (flow < flow_limit) {
            dijkstra(s);
            if (dist[t] == inf) break;
            for (int v = 0; v < n; v++) h[v] += dist[v];
            Cap d = flow_limit - flow;
            for (int v = t; v != s; v = prevv[v]) d = std::min(d, G[prevv[v]][preve[v]].cap);
            for (int v = t; v != s; v = prevv[v]) {
                auto& e = G[prevv[v]][preve[v]];
                e.cap -= d;
                G[v][e.rev].cap += d;
            }
            flow += d;
            cost += d * h[t];
            if (prev_cost_pre_flow == d) res.pop_back();
            res.emplace_back(flow, cost);
            prev_cost_pre_flow = d;
        }
        return res;
    }
};

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    int N, K;
    std::cin >> N >> K;

    PrimalDualonDAG<int, long long> PD(2 * N + 1);
    int s = N, t = 2 * N;
    for (int i = 0; i < N; i++) {
        int A, M;
        std::cin >> A >> M;
        PD.add_edge(N + i, i, K, A);
        PD.add_edge(i, N + i + 1, K, -A);
        PD.add_edge(N + i, N + i + 1, K, 0);
        for (; M--;) {
            int B;
            std::cin >> B;
            PD.add_edge(--B, i, 1, 0);
        }
    }

    long long ans = -PD.min_cost_flow(s, t, K);
    std::cout << ans << '\n';
}
0