結果
| 問題 |
No.1678 Coin Trade (Multiple)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-09-10 22:27:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,309 bytes |
| コンパイル時間 | 2,598 ms |
| コンパイル使用メモリ | 210,680 KB |
| 最終ジャッジ日時 | 2025-01-24 11:30:40 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 WA * 8 TLE * 18 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define each(e, v) for (auto &e : v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
template <typename T>
bool chmax(T &x, const T &y) {
return (x < y) ? (x = y, true) : false;
}
template <typename T>
bool chmin(T &x, const T &y) {
return (x > y) ? (x = y, true) : false;
}
struct io_setup {
io_setup() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
}
} io_setup;
template <typename F, typename T = F>
struct Min_Cost_Flow {
struct edge {
int to;
F cap;
T cost;
int rev;
edge(int to, F cap, T cost, int rev) : to(to), cap(cap), cost(cost), rev(rev) {}
};
vector<vector<edge>> es;
vector<T> d, h;
vector<int> pre_v, pre_e;
bool negative = false;
const F INF_F = numeric_limits<F>::max() / 2;
const T INF_T = numeric_limits<T>::max() / 2;
const int n;
vector<int> deg;
Min_Cost_Flow(int n) : es(n), d(n), h(n), pre_v(n), pre_e(n), n(n), deg(n, 0) {}
void add_edge(int from, int to, F cap, T cost) {
es[from].emplace_back(to, cap, cost, (int)es[to].size());
es[to].emplace_back(from, 0, -cost, (int)es[from].size() - 1);
deg[to]++;
if (cost < 0) negative = true;
}
void bellman_ford(int s) {
fill(begin(h), end(h), INF_T);
h[s] = 0;
queue<int> que;
rep(i, n) {
if (deg[i] == 0) que.emplace(i);
}
while (!empty(que)) {
int i = que.front();
que.pop();
each(e, es[i]) {
if (e.cap == 0) continue;
chmin(h[e.to], h[i] + e.cost);
if (--deg[e.to] == 0) que.emplace(e.to);
}
}
}
void dijkstra(int s) {
fill(begin(d), end(d), INF_T);
using P = pair<T, int>;
priority_queue<P, vector<P>, greater<P>> que;
que.emplace(d[s] = 0, s);
while (!que.empty()) {
auto [p, i] = que.top();
que.pop();
if (p > d[i]) continue;
for (int j = 0; j < (int)es[i].size(); j++) {
edge &e = es[i][j];
if (e.cap > 0 && d[i] + e.cost + h[i] - h[e.to] < d[e.to]) {
d[e.to] = d[i] + e.cost + h[i] - h[e.to];
pre_v[e.to] = i, pre_e[e.to] = j;
que.emplace(d[e.to], e.to);
}
}
}
}
T min_cost_flow(int s, int t, F flow) {
T ret = 0;
if (negative) bellman_ford(s);
while (flow > 0) {
dijkstra(s);
if (d[t] == INF_T) return -1;
for (int i = 0; i < n; i++) {
if (h[i] == INF_T || d[i] == INF_T)
h[i] = INF_T;
else
h[i] += d[i];
}
F f = flow;
for (int now = t; now != s; now = pre_v[now]) { f = min(f, es[pre_v[now]][pre_e[now]].cap); }
ret += h[t] * f, flow -= f;
for (int now = t; now != s; now = pre_v[now]) {
edge &e = es[pre_v[now]][pre_e[now]];
e.cap -= f, es[now][e.rev].cap += f;
}
}
return ret;
}
};
int main() {
int N, K;
cin >> N >> K;
Min_Cost_Flow<ll, int> G(N + 2);
vector<ll> A(N);
int s = N, t = N + 1;
rep(i, N) {
cin >> A[i];
int M;
cin >> M;
rep(j, M) {
int x;
cin >> x;
x--;
G.add_edge(x, i, 1, -A[i] + A[x]);
}
G.add_edge(s, i, K, A[i]);
}
G.add_edge(s, 0, K, 0);
rep(i, N - 1) G.add_edge(i, i + 1, K, 0);
G.add_edge(N - 1, t, K, 0);
cout << -G.min_cost_flow(s, t, K) << '\n';
}