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