結果

問題 No.1676 Coin Trade (Single)
ユーザー beetbeet
提出日時 2021-09-10 23:09:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 3,323 bytes
コンパイル時間 2,606 ms
コンパイル使用メモリ 216,584 KB
実行使用メモリ 26,880 KB
最終ジャッジ日時 2023-09-02 21:14:18
合計ジャッジ時間 5,581 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 102 ms
20,764 KB
testcase_04 AC 95 ms
18,820 KB
testcase_05 AC 79 ms
17,472 KB
testcase_06 AC 81 ms
17,780 KB
testcase_07 AC 65 ms
15,540 KB
testcase_08 AC 36 ms
11,964 KB
testcase_09 AC 63 ms
15,532 KB
testcase_10 AC 67 ms
16,152 KB
testcase_11 AC 106 ms
21,168 KB
testcase_12 AC 53 ms
13,440 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 AC 1 ms
4,368 KB
testcase_15 AC 1 ms
4,368 KB
testcase_16 AC 1 ms
4,372 KB
testcase_17 AC 1 ms
4,368 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 1 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 1 ms
4,372 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 2 ms
4,368 KB
testcase_24 AC 2 ms
4,368 KB
testcase_25 AC 2 ms
4,368 KB
testcase_26 AC 1 ms
4,372 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 1 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 1 ms
4,368 KB
testcase_33 AC 134 ms
26,704 KB
testcase_34 AC 132 ms
26,880 KB
testcase_35 AC 139 ms
26,740 KB
testcase_36 AC 129 ms
26,700 KB
testcase_37 AC 132 ms
26,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


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


//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,k;
  cin>>n>>k;

  PrimalDual<Int, Int> G(n);
  auto init=[&](auto &h)->void{
    fill(h.begin(),h.end(),0);
    auto add_edge=[&](Int u,Int v,Int f,Int c){
      G.add_edge(u,v,f,c);
      chmin(h[v],h[u]+c);
    };
    vector<Int> as(n);
    vector<vector<Int>> H(n);
    for(Int i=0;i<n;i++){
      cin>>as[i];
      Int m;
      cin>>m;
      for(Int j=0;j<m;j++){
        Int p;
        cin>>p;
        p--;
        H[p].emplace_back(i);
      }
    }
    for(Int i=0;i<n;i++){
      for(Int j:H[i])
        if(as[i]<as[j])
          add_edge(i,j,1,as[i]-as[j]);
      if(i+1<n) add_edge(i,i+1,k,0);
    }
  };

  G.build(0,n-1,k,init);
  cout<<-G.get_cost()<<endl;
  return 0;
}
0