結果

問題 No.1678 Coin Trade (Multiple)
ユーザー tokusakuraitokusakurai
提出日時 2021-09-10 22:35:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,589 ms / 5,000 ms
コード長 4,309 bytes
コンパイル時間 3,108 ms
コンパイル使用メモリ 216,940 KB
実行使用メモリ 19,928 KB
最終ジャッジ日時 2023-09-02 19:08:18
合計ジャッジ時間 27,994 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 107 ms
11,292 KB
testcase_04 AC 733 ms
16,776 KB
testcase_05 AC 285 ms
19,072 KB
testcase_06 AC 204 ms
18,208 KB
testcase_07 AC 578 ms
12,892 KB
testcase_08 AC 394 ms
14,332 KB
testcase_09 AC 307 ms
19,176 KB
testcase_10 AC 114 ms
7,332 KB
testcase_11 AC 505 ms
16,032 KB
testcase_12 AC 98 ms
7,456 KB
testcase_13 AC 1,216 ms
18,728 KB
testcase_14 AC 318 ms
10,916 KB
testcase_15 AC 367 ms
15,664 KB
testcase_16 AC 77 ms
15,048 KB
testcase_17 AC 315 ms
19,400 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 1 ms
4,368 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 2 ms
4,368 KB
testcase_24 AC 1 ms
4,368 KB
testcase_25 AC 1 ms
4,372 KB
testcase_26 AC 1 ms
4,368 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 2 ms
4,368 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 ms
4,372 KB
testcase_33 AC 2 ms
4,368 KB
testcase_34 AC 2 ms
4,368 KB
testcase_35 AC 1 ms
4,368 KB
testcase_36 AC 2 ms
4,372 KB
testcase_37 AC 1 ms
4,372 KB
testcase_38 AC 2 ms
4,372 KB
testcase_39 AC 2 ms
4,368 KB
testcase_40 AC 2 ms
4,368 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,376 KB
testcase_45 AC 2 ms
4,372 KB
testcase_46 AC 2 ms
4,368 KB
testcase_47 AC 2 ms
4,372 KB
testcase_48 AC 1,530 ms
19,776 KB
testcase_49 AC 1,563 ms
19,804 KB
testcase_50 AC 1,570 ms
19,644 KB
testcase_51 AC 1,577 ms
19,872 KB
testcase_52 AC 1,572 ms
19,840 KB
testcase_53 AC 1,579 ms
19,676 KB
testcase_54 AC 1,578 ms
19,884 KB
testcase_55 AC 1,589 ms
19,792 KB
testcase_56 AC 1,589 ms
19,908 KB
testcase_57 AC 1,552 ms
19,928 KB
testcase_58 AC 828 ms
16,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define each(e, v) for (auto &e : v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}
template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

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;

    vector<int> deg;

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

    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);
        deg[to]++;
        if (cost < 0) negative = true;
    }

    void bellman_ford(int s) {
        fill(begin(h), end(h), INF_T);
        h[s] = 0;
        queue<int> que;
        rep(i, n) {
            if (deg[i] == 0) que.emplace(i);
        }
        while (!empty(que)) {
            int i = que.front();
            que.pop();
            each(e, es[i]) {
                if (e.cap == 0) continue;
                chmin(h[e.to], h[i] + e.cost);
                if (--deg[e.to] == 0) que.emplace(e.to);
            }
        }
    }

    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;
    }
};

int main() {
    int N, K;
    cin >> N >> K;

    Min_Cost_Flow<int, ll> G(N + 2);

    vector<ll> A(N);
    int s = N, t = N + 1;

    rep(i, N) {
        cin >> A[i];
        int M;
        cin >> M;
        rep(j, M) {
            int x;
            cin >> x;
            x--;
            G.add_edge(x, i, 1, -A[i] + A[x]);
        }
        G.add_edge(s, i, K, A[i]);
    }

    G.add_edge(s, 0, K, 0);
    rep(i, N - 1) G.add_edge(i, i + 1, K, 0);
    G.add_edge(N - 1, t, K, 0);

    cout << -G.min_cost_flow(s, t, K) << '\n';
}
0