結果
| 問題 |
No.1678 Coin Trade (Multiple)
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2021-09-10 23:09:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 536 ms / 5,000 ms |
| コード長 | 3,323 bytes |
| コンパイル時間 | 2,457 ms |
| コンパイル使用メモリ | 209,632 KB |
| 最終ジャッジ日時 | 2025-01-24 12:12:55 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 56 |
ソースコード
#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;
}
beet