結果
| 問題 |
No.1676 Coin Trade (Single)
|
| コンテスト | |
| ユーザー |
sapphire__15
|
| 提出日時 | 2021-09-10 23:03:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 4,695 bytes |
| コンパイル時間 | 1,657 ms |
| コンパイル使用メモリ | 121,788 KB |
| 最終ジャッジ日時 | 2025-01-24 12:10:28 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 TLE * 5 |
ソースコード
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <cmath>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#define rep(i,n,s) for(int i = (s); i < int(n); i++)
#define MM << " " <<
#define all(x) x.begin(), x.end()
template<class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
template<class T>
bool chmin(T& a, const T b) {
if(a > b) {
a = b;
return true;
} else {
return false;
}
}
template<class T>
bool chmax(T& a, const T b) {
if(a < b) {
a = b;
return true;
} else {
return false;
}
}
template<class T>
void vdeb(std::vector<T> &da) {
auto n = da.size();
for(size_t i = 0; i < n; i++) {
if(i == n-1) std::cout << da[i] << std::endl;
else std::cout << da[i] << " ";
}
}
template<>
void vdeb(std::vector<std::string> &da) {
auto n = da.size();
for(size_t i = 0; i < n; i++) {
std::cout << da[i] << std::endl;
}
}
using namespace std;
// the class of solver for min const from problem
// author: tokusakurai
// refernce: https://github.com/tokusakurai/Library/blob/main/Graph/Primal-Dual-2.hpp
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;
Min_Cost_Flow(int n) : es(n), d(n), h(n), pre_v(n), pre_e(n), n(n) {}
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);
if (cost < 0) negative = true;
}
void bellman_ford(int s) {
fill(begin(h), end(h), INF_T);
h[s] = 0;
while (true) {
bool update = false;
for (int i = 0; i < n; i++) {
if (h[i] == INF_T) continue;
for (auto &e : es[i]) {
if (e.cap > 0 && h[i] + e.cost < h[e.to]) {
h[e.to] = h[i] + e.cost;
update = true;
}
}
}
if (!update) break;
}
}
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;
}
};
const int INF = 100;
int main() {
int n, k; cin >> n >> k;
Min_Cost_Flow<int, ll> mcf(n*2+2);
mcf.add_edge(0, 1, INF, 0);
rep(i,n,0) {
mcf.add_edge(i*2+1, i*2+3, INF, 0);
ll a; cin >> a;
mcf.add_edge(i*2+2, i*2+1, INF, -a);
mcf.add_edge(i*2+1, i*2+2, INF, a);
int m; cin >> m;
rep(j,m,0) {
int b; cin >> b;
--b;
mcf.add_edge(b*2+2, i*2+2, 1, 0);
}
}
auto ans = mcf.min_cost_flow(0, n*2+1, k);
cout << -ans << endl;
}
sapphire__15