結果

問題 No.1678 Coin Trade (Multiple)
ユーザー mugen_1337mugen_1337
提出日時 2021-09-10 22:37:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,580 ms / 5,000 ms
コード長 3,942 bytes
コンパイル時間 3,713 ms
コンパイル使用メモリ 222,948 KB
実行使用メモリ 19,376 KB
最終ジャッジ日時 2023-09-02 19:18:21
合計ジャッジ時間 26,594 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 85 ms
10,036 KB
testcase_04 AC 679 ms
15,324 KB
testcase_05 AC 268 ms
18,488 KB
testcase_06 AC 191 ms
17,172 KB
testcase_07 AC 507 ms
11,156 KB
testcase_08 AC 326 ms
12,300 KB
testcase_09 AC 283 ms
18,684 KB
testcase_10 AC 80 ms
5,756 KB
testcase_11 AC 445 ms
14,652 KB
testcase_12 AC 63 ms
6,032 KB
testcase_13 AC 1,176 ms
17,544 KB
testcase_14 AC 247 ms
9,628 KB
testcase_15 AC 372 ms
14,200 KB
testcase_16 AC 73 ms
14,288 KB
testcase_17 AC 309 ms
18,868 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 1 ms
4,372 KB
testcase_21 AC 1 ms
4,368 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 1 ms
4,372 KB
testcase_24 AC 2 ms
4,368 KB
testcase_25 AC 2 ms
4,368 KB
testcase_26 AC 2 ms
4,372 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 1 ms
4,372 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 2 ms
4,368 KB
testcase_31 AC 2 ms
4,368 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 2 ms
4,368 KB
testcase_34 AC 1 ms
4,372 KB
testcase_35 AC 2 ms
4,372 KB
testcase_36 AC 2 ms
4,368 KB
testcase_37 AC 2 ms
4,368 KB
testcase_38 AC 2 ms
4,372 KB
testcase_39 AC 2 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,372 KB
testcase_44 AC 2 ms
4,368 KB
testcase_45 AC 1 ms
4,368 KB
testcase_46 AC 2 ms
4,368 KB
testcase_47 AC 2 ms
4,368 KB
testcase_48 AC 1,548 ms
19,208 KB
testcase_49 AC 1,512 ms
19,316 KB
testcase_50 AC 1,512 ms
19,256 KB
testcase_51 AC 1,580 ms
19,180 KB
testcase_52 AC 1,569 ms
19,376 KB
testcase_53 AC 1,502 ms
19,296 KB
testcase_54 AC 1,489 ms
19,168 KB
testcase_55 AC 1,501 ms
19,200 KB
testcase_56 AC 1,534 ms
19,264 KB
testcase_57 AC 1,494 ms
19,288 KB
testcase_58 AC 848 ms
19,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

// O(F E \log V)
template<typename Flow, typename Cost>
struct PrimalDual{
  struct Edge{
    int dst;
    Flow cap;
    Cost cost;
    int rev;
    Edge(int dst,Flow cap,Cost cost,int rev):
      dst(dst),cap(cap),cost(cost),rev(rev){}
  };
 
  vector<vector<Edge>> G;
  vector<Cost> h,dist;
  vector<int> prevv,preve;
 
  PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){}
 
  void add_edge(int u,int v,Flow cap,Cost cost){
    int e=G[u].size();
    int r=(u==v?e+1:G[v].size());
    G[u].emplace_back(v,cap,cost,r);
    G[v].emplace_back(u,0,-cost,e);
  }
 
  Cost residual_cost(int src,Edge &e){
    return e.cost+h[src]-h[e.dst];
  }
 
  void dijkstra(int s){
    struct P{
      Cost first;
      int second;
      P(Cost first,int second):first(first),second(second){}
      bool operator<(const P&a) const{return first>a.first;}
    };
    priority_queue<P> pq;
 
    dist[s]=0;
    pq.emplace(dist[s],s);
    while(!pq.empty()){
      P p=pq.top();pq.pop();
      int v=p.second;
      if(dist[v]<p.first) continue;
      for(int i=0;i<(int)G[v].size();i++){
        Edge &e=G[v][i];
        if(e.cap==0) continue;
        if(!(dist[v]+residual_cost(v,e)<dist[e.dst])) continue;
        dist[e.dst]=dist[v]+e.cost+h[v]-h[e.dst];
        prevv[e.dst]=v;
        preve[e.dst]=i;
        pq.emplace(dist[e.dst],e.dst);
      }
    }
  }
 
  Cost res;
 
  bool build(int s,int t,Flow f,
             function<void(decltype(h)&)> init=[](decltype(h) &p){
               fill(p.begin(),p.end(),0);
             }){
    res=0;
    init(h);
    const Cost INF = numeric_limits<Cost>::max();
    while(f>0){
      fill(dist.begin(),dist.end(),INF);
      dijkstra(s);
      if(dist[t]==INF) return false;
 
      for(int v=0;v<(int)h.size();v++)
        if(dist[v]<INF) h[v]=h[v]+dist[v];
 
      Flow d=f;
      for(int v=t;v!=s;v=prevv[v])
        d=min(d,G[prevv[v]][preve[v]].cap);
 
      f-=d;
      res=res+h[t]*d;
      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 true;
  }
 
  Cost get_cost(){return res;}
};

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

    PrimalDual<ll,ll> flow(N+N+40);
    int src=N+N,sink=N+N+1;

    vector<int> A(N);
    vector<ll> h(N+N+40,0);

    auto add_edge=[&](int u,int v,int cap,int cost){
        flow.add_edge(u,v,cap,cost);
        chmin(h[v],h[u]+cost);
    };

    rep(i,N){
        int t;cin>>A[i]>>t;
        rep(j,t){
            int a;cin>>a;a--;
            if(A[i]-A[a]>0){
                add_edge(a+N,i,1,-(A[i]-A[a]));
            }
        }
        add_edge(i,i+N,K,0);
        if(i+1<N) add_edge(i+N,i+1,K,0);
    }

    add_edge(src,0,K,0);
    add_edge(N+N-1,sink,K,0);
    add_edge(src,sink,K,0);

    auto init=[&](auto &wa)->void{
        wa=move(h);
    };

    int mx=K;
    flow.build(src,sink,mx,init);
    cout<<-flow.get_cost()<<endl;
    return 0;
}
0