結果

問題 No.1678 Coin Trade (Multiple)
ユーザー sapphire__15sapphire__15
提出日時 2021-09-10 23:02:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,220 ms / 5,000 ms
コード長 4,695 bytes
コンパイル時間 1,476 ms
コンパイル使用メモリ 126,632 KB
実行使用メモリ 22,032 KB
最終ジャッジ日時 2023-09-02 20:57:10
合計ジャッジ時間 37,433 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 271 ms
11,924 KB
testcase_04 AC 1,161 ms
17,988 KB
testcase_05 AC 823 ms
21,152 KB
testcase_06 AC 686 ms
19,616 KB
testcase_07 AC 910 ms
13,528 KB
testcase_08 AC 692 ms
14,584 KB
testcase_09 AC 887 ms
21,236 KB
testcase_10 AC 134 ms
7,248 KB
testcase_11 AC 924 ms
17,108 KB
testcase_12 AC 125 ms
7,172 KB
testcase_13 AC 1,808 ms
20,272 KB
testcase_14 AC 476 ms
11,744 KB
testcase_15 AC 677 ms
16,700 KB
testcase_16 AC 373 ms
16,300 KB
testcase_17 AC 836 ms
21,580 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 1 ms
4,368 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 1 ms
4,368 KB
testcase_24 AC 2 ms
4,368 KB
testcase_25 AC 2 ms
4,372 KB
testcase_26 AC 2 ms
4,372 KB
testcase_27 AC 2 ms
4,368 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 2 ms
4,368 KB
testcase_30 AC 1 ms
4,368 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 2 ms
4,372 KB
testcase_35 AC 2 ms
4,368 KB
testcase_36 AC 2 ms
4,372 KB
testcase_37 AC 2 ms
4,372 KB
testcase_38 AC 2 ms
4,368 KB
testcase_39 AC 1 ms
4,368 KB
testcase_40 AC 2 ms
4,372 KB
testcase_41 AC 1 ms
4,372 KB
testcase_42 AC 2 ms
4,372 KB
testcase_43 AC 2 ms
4,368 KB
testcase_44 AC 2 ms
4,368 KB
testcase_45 AC 2 ms
4,372 KB
testcase_46 AC 2 ms
4,372 KB
testcase_47 AC 2 ms
4,372 KB
testcase_48 AC 2,187 ms
21,968 KB
testcase_49 AC 2,188 ms
21,884 KB
testcase_50 AC 2,123 ms
21,836 KB
testcase_51 AC 2,158 ms
21,872 KB
testcase_52 AC 2,183 ms
21,892 KB
testcase_53 AC 2,093 ms
22,032 KB
testcase_54 AC 2,122 ms
21,928 KB
testcase_55 AC 2,160 ms
21,940 KB
testcase_56 AC 2,205 ms
22,004 KB
testcase_57 AC 2,220 ms
21,880 KB
testcase_58 AC 770 ms
19,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <cmath>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>

#define rep(i,n,s) for(int i = (s); i < int(n); i++)
#define MM << " " << 
#define all(x) x.begin(), x.end()

template<class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;

template<class T>
bool chmin(T& a, const T b) {
    if(a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template<class T>
bool chmax(T& a, const T b) {
    if(a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template<class T>
void vdeb(std::vector<T> &da) {
    auto n = da.size();
    for(size_t i = 0; i < n; i++) {
        if(i == n-1) std::cout << da[i] << std::endl;
        else std::cout << da[i] << " ";
    }
}
template<>
void vdeb(std::vector<std::string> &da) {
    auto n = da.size();
    for(size_t i = 0; i < n; i++) {
        std::cout << da[i] << std::endl;
    }
}

using namespace std;

// the class of solver for min const from problem
// author: tokusakurai
// refernce: https://github.com/tokusakurai/Library/blob/main/Graph/Primal-Dual-2.hpp
template <typename F, typename T = F> // 流量の型、費用の型
struct Min_Cost_Flow {
    struct edge {
        int to;
        F cap;
        T cost;
        int rev;
        edge(int to, F cap, T cost, int rev) : to(to), cap(cap), cost(cost), rev(rev) {}
    };

    vector<vector<edge>> es;
    vector<T> d, h;
    vector<int> pre_v, pre_e;
    bool negative = false;
    const F INF_F = numeric_limits<F>::max() / 2;
    const T INF_T = numeric_limits<T>::max() / 2;
    const int n;

    Min_Cost_Flow(int n) : es(n), d(n), h(n), pre_v(n), pre_e(n), n(n) {}

    void add_edge(int from, int to, F cap, T cost) {
        es[from].emplace_back(to, cap, cost, (int)es[to].size());
        es[to].emplace_back(from, 0, -cost, (int)es[from].size() - 1);
        if (cost < 0) negative = true;
    }

    void bellman_ford(int s) {
        fill(begin(h), end(h), INF_T);
        h[s] = 0;
        while (true) {
            bool update = false;
            for (int i = 0; i < n; i++) {
                if (h[i] == INF_T) continue;
                for (auto &e : es[i]) {
                    if (e.cap > 0 && h[i] + e.cost < h[e.to]) {
                        h[e.to] = h[i] + e.cost;
                        update = true;
                    }
                }
            }
            if (!update) break;
        }
    }

    void dijkstra(int s) {
        fill(begin(d), end(d), INF_T);
        using P = pair<T, int>;
        priority_queue<P, vector<P>, greater<P>> que;
        que.emplace(d[s] = 0, s);
        while (!que.empty()) {
            auto [p, i] = que.top();
            que.pop();
            if (p > d[i]) continue;
            for (int j = 0; j < (int)es[i].size(); j++) {
                edge &e = es[i][j];
                if (e.cap > 0 && d[i] + e.cost + h[i] - h[e.to] < d[e.to]) {
                    d[e.to] = d[i] + e.cost + h[i] - h[e.to];
                    pre_v[e.to] = i, pre_e[e.to] = j;
                    que.emplace(d[e.to], e.to);
                }
            }
        }
    }

    T min_cost_flow(int s, int t, F flow) {
        T ret = 0;
        if (negative) bellman_ford(s);
        while (flow > 0) {
            dijkstra(s);
            if (d[t] == INF_T) return -1;
            for (int i = 0; i < n; i++) {
                if (h[i] == INF_T || d[i] == INF_T)
                    h[i] = INF_T;
                else
                    h[i] += d[i];
            }
            F f = flow;
            for (int now = t; now != s; now = pre_v[now]) { f = min(f, es[pre_v[now]][pre_e[now]].cap); }
            ret += h[t] * f, flow -= f;
            for (int now = t; now != s; now = pre_v[now]) {
                edge &e = es[pre_v[now]][pre_e[now]];
                e.cap -= f, es[now][e.rev].cap += f;
            }
        }
        return ret;
    }
};

const int INF = 100;

int main() {
    int n, k; cin >> n >> k;
    Min_Cost_Flow<int, ll> mcf(n*2+2);
    mcf.add_edge(0, 1, INF, 0);
    rep(i,n,0) {
        mcf.add_edge(i*2+1, i*2+3, INF, 0);
        ll a; cin >> a;
        mcf.add_edge(i*2+2, i*2+1, INF, -a);
        mcf.add_edge(i*2+1, i*2+2, INF, a);
        int m; cin >> m;
        rep(j,m,0) {
            int b; cin >> b;
            --b;
            mcf.add_edge(b*2+2, i*2+2, 1, 0);
        }
    }
    auto ans = mcf.min_cost_flow(0, n*2+1, k);
    cout << -ans << endl;
}
0