結果
問題 | No.1678 Coin Trade (Multiple) |
ユーザー | sapphire__15 |
提出日時 | 2021-09-10 23:02:52 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,458 ms / 5,000 ms |
コード長 | 4,695 bytes |
コンパイル時間 | 1,454 ms |
コンパイル使用メモリ | 128,512 KB |
実行使用メモリ | 21,976 KB |
最終ジャッジ日時 | 2024-06-12 03:16:06 |
合計ジャッジ時間 | 39,842 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 282 ms
12,316 KB |
testcase_04 | AC | 1,301 ms
17,952 KB |
testcase_05 | AC | 944 ms
21,116 KB |
testcase_06 | AC | 788 ms
19,696 KB |
testcase_07 | AC | 992 ms
13,604 KB |
testcase_08 | AC | 704 ms
14,724 KB |
testcase_09 | AC | 921 ms
21,336 KB |
testcase_10 | AC | 132 ms
7,532 KB |
testcase_11 | AC | 956 ms
17,100 KB |
testcase_12 | AC | 126 ms
7,592 KB |
testcase_13 | AC | 1,892 ms
20,240 KB |
testcase_14 | AC | 511 ms
11,976 KB |
testcase_15 | AC | 766 ms
16,700 KB |
testcase_16 | AC | 443 ms
16,568 KB |
testcase_17 | AC | 982 ms
21,680 KB |
testcase_18 | AC | 2 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 2 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 3 ms
5,376 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 2 ms
5,376 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 3 ms
5,376 KB |
testcase_31 | AC | 2 ms
5,376 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 2 ms
5,376 KB |
testcase_34 | AC | 2 ms
5,376 KB |
testcase_35 | AC | 2 ms
5,376 KB |
testcase_36 | AC | 2 ms
5,376 KB |
testcase_37 | AC | 3 ms
5,376 KB |
testcase_38 | AC | 2 ms
5,376 KB |
testcase_39 | AC | 2 ms
5,376 KB |
testcase_40 | AC | 2 ms
5,376 KB |
testcase_41 | AC | 2 ms
5,376 KB |
testcase_42 | AC | 2 ms
5,376 KB |
testcase_43 | AC | 2 ms
5,376 KB |
testcase_44 | AC | 2 ms
5,376 KB |
testcase_45 | AC | 2 ms
5,376 KB |
testcase_46 | AC | 3 ms
5,376 KB |
testcase_47 | AC | 2 ms
5,376 KB |
testcase_48 | AC | 2,424 ms
21,968 KB |
testcase_49 | AC | 2,322 ms
21,964 KB |
testcase_50 | AC | 2,344 ms
21,972 KB |
testcase_51 | AC | 2,416 ms
21,952 KB |
testcase_52 | AC | 2,421 ms
21,976 KB |
testcase_53 | AC | 2,397 ms
21,968 KB |
testcase_54 | AC | 2,364 ms
21,956 KB |
testcase_55 | AC | 2,368 ms
21,976 KB |
testcase_56 | AC | 2,382 ms
21,968 KB |
testcase_57 | AC | 2,458 ms
21,968 KB |
testcase_58 | AC | 784 ms
19,356 KB |
ソースコード
#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; }