結果

問題 No.1678 Coin Trade (Multiple)
ユーザー fumofumofunifumofumofuni
提出日時 2021-09-10 22:48:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,337 bytes
コンパイル時間 2,227 ms
コンパイル使用メモリ 212,636 KB
実行使用メモリ 19,108 KB
最終ジャッジ日時 2023-09-02 20:01:34
合計ジャッジ時間 13,656 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 4,510 ms
12,644 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
//const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
} 


using Flow = ll;
using Cost = ll;
const int MAX_V = 50010;
const Cost INF = std::numeric_limits<Cost>::max() / 8;

struct PrimalDual {
    struct Edge {
        int d;
        Flow c, f;
        Cost w;
        int r, is_r;
        Edge(int d_, Flow c_, Flow f_, Cost w_, int r_, bool is_r_)
            : d(d_), c(c_), f(f_), w(w_), r(r_), is_r(is_r_) {}
    };

    int n;
    std::vector<std::vector<Edge> > g;
    PrimalDual(int n_) : n(n_), g(std::vector<std::vector<Edge> >(n_)) {}

    void add_edge(int src, int dst, Flow cap, Cost cost) {  // 有向辺
        int rsrc = g[dst].size();
        int rdst = g[src].size();
        g[src].emplace_back(dst, cap, 0, cost, rsrc, false);
        g[dst].emplace_back(src, cap, cap, -cost, rdst, true);
    }

    Cost solve(int s, int t, Flow f) {
        Cost res = 0;

        static Cost h[MAX_V + 10], dist[MAX_V];
        static int prevv[MAX_V + 10], preve[MAX_V + 10];
        // std::vector<Cost> h(g.size()), dist(g.size());
        // std::vector<int> prevv(g.size()), preve(g.size());

        using pcv = std::pair<Cost, int>;
        std::priority_queue<pcv, std::vector<pcv>, std::greater<pcv> > q;
        std::fill(h, h + n, 0);
        while (f > 0) {
            std::fill(dist, dist + n, INF);
            dist[s] = 0;
            q.emplace(0, s);
            while (q.size()) {
                Cost cd;
                int v;
                std::tie(cd, v) = q.top();
                q.pop();
                if (dist[v] < cd) continue;
                for (int i = 0; i < (int)(g[v].size()); ++i) {
                    Edge &e = g[v][i];
                    if (residue(e) == 0) continue;
                    if (dist[e.d] + h[e.d] > cd + h[v] + e.w) {
                        dist[e.d] = dist[v] + e.w + h[v] - h[e.d];
                        prevv[e.d] = v;
                        preve[e.d] = i;
                        q.emplace(dist[e.d], e.d);
                    }
                }
            }

            if (dist[t] == INF) return -1;  // 経路が見つからなかった

            // s-t 間最短路に沿って目一杯流す
            for (int i = 0; i < n; ++i) h[i] += dist[i];
            Flow d = f;
            for (int v = t; v != s; v = prevv[v]) {
                d = std::min(d, residue(g[prevv[v]][preve[v]]));
            }
            f -= d;
            res += d * h[t];
            for (int v = t; v != s; v = prevv[v]) {
                Edge &e = g[prevv[v]][preve[v]];
                e.f += d;
                g[v][e.r].f -= d;
            }
        }
        return res;
    }

    Flow residue(const Edge &e) { return e.c - e.f; }

    // 流量を表示
    void show() {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < (int)(g[i].size()); ++j) {
                Edge &e = g[i][j];
                if (e.is_r) continue;
                printf("%3d->%3d (flow:%d)\n", i, e.d, e.f);
            }
        }
    }
};

int main(){
    ll n,k;cin >> n >> k;
    PrimalDual pd(n);
    vl a(n);
    rep(i,n){
        cin >> a[i];
        ll m;cin >> m;
        rep(j,m){
            ll t;cin >> t;t--;
            pd.add_edge(t,i,1,a[t]-a[i]);
        }
    }
    rep(i,n-1)pd.add_edge(i,i+1,inf,0);
    cout << -pd.solve(0,n-1,k) << endl;
}

0