結果
| 問題 |
No.1678 Coin Trade (Multiple)
|
| コンテスト | |
| ユーザー |
mugen_1337
|
| 提出日時 | 2021-09-10 22:16:21 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,820 bytes |
| コンパイル時間 | 2,417 ms |
| コンパイル使用メモリ | 209,228 KB |
| 最終ジャッジ日時 | 2025-01-24 11:19:36 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 TLE * 20 |
ソースコード
#include<bits/stdc++.h>
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;
flow.add_edge(src,0,K,0);
flow.add_edge(N+N-1,sink,K,0);
rep(i,N){
flow.add_edge(i,i+N,K,0);
if(i+1<N) flow.add_edge(i+N,i+1,K,0);
}
vector<int> A(N);
vector<ll> h(N+N+40,0);
rep(i,N){
int t;cin>>A[i]>>t;
rep(j,t){
int a;cin>>a;a--;
if(A[i]-A[a]>0){
flow.add_edge(a+N,i,1,-(A[i]-A[a]));
chmin(h[a+N],h[i]-(A[i]-A[a]));
}
}
}
flow.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;
}
mugen_1337