結果
| 問題 |
No.1678 Coin Trade (Multiple)
|
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2021-08-01 21:17:30 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 2,610 bytes |
| コンパイル時間 | 3,238 ms |
| コンパイル使用メモリ | 189,052 KB |
| 最終ジャッジ日時 | 2025-01-23 13:27:22 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 TLE * 23 |
ソースコード
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct edge{
int to, cap, rev; ll cost;
edge(int to, int cap, ll cost, int rev):to(to), cap(cap), cost(cost), rev(rev){}
};
const int MAXV=100010;
int V;
vector<edge> g[MAXV];
ll h[MAXV];
ll dist[MAXV];
int prevv[MAXV], preve[MAXV];
void add_edge(int from, int to, int cap, ll cost){
edge e=edge(to, cap, cost, g[to].size());
g[from].push_back(e);
e=edge(from, 0, -cost, g[from].size()-1);
g[to].push_back(e);
}
ll min_cost_flow(int s, int t, int f){
using P=pair<ll, int>;
const ll INF=1e18;
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({0, s});
while(!que.empty()){
P p=que.top(); que.pop();
int v=p.second;
if(dist[v]<p.first) continue;
for(int i=0; i<g[v].size(); i++){
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({dist[e.to], e.to});
}
}
}
for(int v=0; v<V; v++) h[v]+=dist[v];
if(dist[t]==INF) return -1;
int 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;
}
int main()
{
int n, k;
cin>>n>>k;
V=2*n+1;
ll a[100010];
for(int i=0; i<n; i++){
cin>>a[i];
int m; cin>>m;
for(int j=0; j<m; j++){
int b; cin>>b; b--;
add_edge(2*b+1, 2*i+1, 1, 0);
}
}
for(int i=0; i<n; i++){
add_edge(2*i, 2*i+1, k, a[i]);
add_edge(2*i+1, 2*i+2, k, -a[i]);
add_edge(2*i, 2*i+2, k, 0);
}
cout<<-min_cost_flow(0, 2*n, k)<<endl;
return 0;
}
chocorusk