結果

問題 No.1678 Coin Trade (Multiple)
ユーザー abc3abc3
提出日時 2021-09-11 20:55:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 653 ms / 5,000 ms
コード長 2,896 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 212,708 KB
実行使用メモリ 15,548 KB
最終ジャッジ日時 2023-09-05 09:57:27
合計ジャッジ時間 15,299 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,568 KB
testcase_01 AC 2 ms
4,560 KB
testcase_02 AC 2 ms
4,608 KB
testcase_03 AC 57 ms
9,828 KB
testcase_04 AC 333 ms
13,092 KB
testcase_05 AC 134 ms
14,900 KB
testcase_06 AC 99 ms
14,116 KB
testcase_07 AC 285 ms
10,596 KB
testcase_08 AC 184 ms
11,312 KB
testcase_09 AC 143 ms
15,108 KB
testcase_10 AC 77 ms
7,216 KB
testcase_11 AC 225 ms
12,724 KB
testcase_12 AC 63 ms
7,220 KB
testcase_13 AC 492 ms
14,364 KB
testcase_14 AC 151 ms
9,836 KB
testcase_15 AC 164 ms
12,544 KB
testcase_16 AC 45 ms
12,396 KB
testcase_17 AC 139 ms
15,232 KB
testcase_18 AC 2 ms
4,576 KB
testcase_19 AC 2 ms
4,564 KB
testcase_20 AC 2 ms
4,564 KB
testcase_21 AC 2 ms
4,740 KB
testcase_22 AC 2 ms
4,588 KB
testcase_23 AC 2 ms
4,700 KB
testcase_24 AC 2 ms
4,624 KB
testcase_25 AC 2 ms
4,676 KB
testcase_26 AC 3 ms
4,616 KB
testcase_27 AC 2 ms
4,568 KB
testcase_28 AC 3 ms
4,568 KB
testcase_29 AC 3 ms
4,800 KB
testcase_30 AC 2 ms
4,560 KB
testcase_31 AC 2 ms
4,564 KB
testcase_32 AC 3 ms
4,556 KB
testcase_33 AC 2 ms
4,568 KB
testcase_34 AC 2 ms
4,604 KB
testcase_35 AC 2 ms
4,620 KB
testcase_36 AC 2 ms
4,552 KB
testcase_37 AC 2 ms
4,624 KB
testcase_38 AC 2 ms
4,572 KB
testcase_39 AC 2 ms
4,592 KB
testcase_40 AC 3 ms
4,568 KB
testcase_41 AC 2 ms
4,612 KB
testcase_42 AC 3 ms
4,628 KB
testcase_43 AC 2 ms
4,624 KB
testcase_44 AC 2 ms
4,556 KB
testcase_45 AC 2 ms
4,560 KB
testcase_46 AC 2 ms
4,588 KB
testcase_47 AC 2 ms
4,560 KB
testcase_48 AC 653 ms
15,424 KB
testcase_49 AC 629 ms
15,376 KB
testcase_50 AC 617 ms
15,392 KB
testcase_51 AC 623 ms
15,548 KB
testcase_52 AC 609 ms
15,360 KB
testcase_53 AC 616 ms
15,360 KB
testcase_54 AC 614 ms
15,348 KB
testcase_55 AC 615 ms
15,448 KB
testcase_56 AC 623 ms
15,424 KB
testcase_57 AC 630 ms
15,440 KB
testcase_58 AC 404 ms
15,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

   #include <bits/stdc++.h>
    using namespace std;
    #include <math.h>
    #include <iomanip>
    #include <cstdint>
    #include <string>
    #include <sstream>
    template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
    #define rep(i,n) for (int i = 0; i < (n); ++i)
    typedef long long ll;
    typedef unsigned long long ull;
    using P=pair<ll,ll>;
    const ll INF=1e18;
    const int mod=1e9+7;

    const int MAX_V=50005;
    
    struct edge{
        ll to,cap,cost,rev;
    };

    ll V;
    vector<edge>G[MAX_V];
    ll h[MAX_V];
    ll dist[MAX_V];
    ll prevv[MAX_V],preve[MAX_V];

    void add_edge(ll from,ll to,ll cap,ll cost){
        G[from].push_back((edge){to,cap,cost,(int)G[to].size()});
        G[to].push_back((edge){from,0,-cost,(int)G[from].size()-1});
    }

    ll min_cost_flow(int s,int t,ll f){
        ll res=0;
        fill(h,h+V,0);
        while(f>0){
            priority_queue<P,vector<P>,greater<P>>que;
            fill(dist,dist+V,INF);
            dist[s]=0;
            que.push(P(0,s));
            while(!que.empty()){
                P p=que.top();que.pop();
                int v=p.second;
                if(dist[v]<p.first){continue;}
                rep(i,G[v].size()){
                    edge &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;
                        que.push(P(dist[e.to],e.to));
                    }
                }
            }
            if(dist[t]==INF){return -1;}
            rep(v,V){h[v]+=dist[v];}
            
            ll d=f;
            for(int v=t;v!=s;v=prevv[v]){
                d=min(d,G[prevv[v]][preve[v]].cap);
            }
            f-=d;
            res+=d*h[t];
            for(int v=t;v!=s;v=prevv[v]){
                edge &e=G[prevv[v]][preve[v]];
                e.cap-=d;
                G[v][e.rev].cap+=d;
            }
        }
        return res;
    }
    void solve(){
        ll k;
        cin>>V>>k;
        vector<ll>a(V);
        vector<vector<int>>b(V);
        ll mx=1e9;
        rep(i,V){
            int m;
            cin>>a[i]>>m;
            b[i].resize(m);
            rep(j,m){
                cin>>b[i][j];
                b[i][j]--;
            }
        }
        rep(i,V){
            for(auto j:b[i]){
                if(a[i]>a[j]){add_edge(j,i,1,a[j]-a[i]+((ll)i-(ll)j)*mx);}
            }
            if(i+1<V){add_edge(i,i+1,k,mx);}
        }
        cout<<-min_cost_flow(0,V-1,k)+mx*(V-1)*k<<endl;
    }
 
    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
        solve(); 

        return 0;
    }
0