結果

問題 No.1678 Coin Trade (Multiple)
ユーザー rniyarniya
提出日時 2024-04-07 17:41:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,301 ms / 5,000 ms
コード長 5,611 bytes
コンパイル時間 1,448 ms
コンパイル使用メモリ 104,388 KB
実行使用メモリ 24,404 KB
最終ジャッジ日時 2024-10-01 04:36:46
合計ジャッジ時間 18,083 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 79 ms
13,292 KB
testcase_04 AC 472 ms
19,780 KB
testcase_05 AC 194 ms
23,488 KB
testcase_06 AC 150 ms
21,772 KB
testcase_07 AC 392 ms
14,776 KB
testcase_08 AC 271 ms
16,060 KB
testcase_09 AC 244 ms
23,396 KB
testcase_10 AC 67 ms
7,904 KB
testcase_11 AC 413 ms
18,864 KB
testcase_12 AC 63 ms
7,992 KB
testcase_13 AC 1,079 ms
22,428 KB
testcase_14 AC 220 ms
12,844 KB
testcase_15 AC 251 ms
18,328 KB
testcase_16 AC 72 ms
18,328 KB
testcase_17 AC 256 ms
23,876 KB
testcase_18 AC 2 ms
6,820 KB
testcase_19 AC 2 ms
6,816 KB
testcase_20 AC 2 ms
6,820 KB
testcase_21 AC 1 ms
6,820 KB
testcase_22 AC 2 ms
6,816 KB
testcase_23 AC 1 ms
6,820 KB
testcase_24 AC 2 ms
6,820 KB
testcase_25 AC 2 ms
6,816 KB
testcase_26 AC 2 ms
6,816 KB
testcase_27 AC 2 ms
6,820 KB
testcase_28 AC 2 ms
6,820 KB
testcase_29 AC 2 ms
6,816 KB
testcase_30 AC 1 ms
6,824 KB
testcase_31 AC 2 ms
6,820 KB
testcase_32 AC 2 ms
6,816 KB
testcase_33 AC 1 ms
6,820 KB
testcase_34 AC 2 ms
6,820 KB
testcase_35 AC 2 ms
6,820 KB
testcase_36 AC 1 ms
6,820 KB
testcase_37 AC 2 ms
6,820 KB
testcase_38 AC 2 ms
6,820 KB
testcase_39 AC 2 ms
6,816 KB
testcase_40 AC 2 ms
6,816 KB
testcase_41 AC 1 ms
6,820 KB
testcase_42 AC 2 ms
6,820 KB
testcase_43 AC 1 ms
6,820 KB
testcase_44 AC 2 ms
6,820 KB
testcase_45 AC 2 ms
6,820 KB
testcase_46 AC 2 ms
6,820 KB
testcase_47 AC 1 ms
6,820 KB
testcase_48 AC 1,301 ms
24,396 KB
testcase_49 AC 1,089 ms
24,392 KB
testcase_50 AC 1,030 ms
24,268 KB
testcase_51 AC 1,059 ms
24,376 KB
testcase_52 AC 1,020 ms
24,404 KB
testcase_53 AC 1,015 ms
24,396 KB
testcase_54 AC 1,068 ms
24,388 KB
testcase_55 AC 1,051 ms
24,392 KB
testcase_56 AC 1,012 ms
24,392 KB
testcase_57 AC 1,021 ms
24,392 KB
testcase_58 AC 595 ms
21,396 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