結果
問題 | No.1676 Coin Trade (Single) |
ユーザー | sapphire__15 |
提出日時 | 2021-09-10 23:03:47 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,512 ms / 2,000 ms |
コード長 | 4,695 bytes |
コンパイル時間 | 1,495 ms |
コンパイル使用メモリ | 127,012 KB |
実行使用メモリ | 39,644 KB |
最終ジャッジ日時 | 2024-06-12 03:17:40 |
合計ジャッジ時間 | 17,362 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1,038 ms
30,340 KB |
testcase_04 | AC | 913 ms
27,552 KB |
testcase_05 | AC | 833 ms
25,412 KB |
testcase_06 | AC | 856 ms
25,716 KB |
testcase_07 | AC | 638 ms
22,064 KB |
testcase_08 | AC | 305 ms
15,724 KB |
testcase_09 | AC | 677 ms
21,664 KB |
testcase_10 | AC | 723 ms
22,628 KB |
testcase_11 | AC | 1,143 ms
31,244 KB |
testcase_12 | AC | 447 ms
18,308 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 1 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 1 ms
5,376 KB |
testcase_22 | AC | 1 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 | 1 ms
5,376 KB |
testcase_27 | AC | 1 ms
5,376 KB |
testcase_28 | AC | 2 ms
5,376 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 2 ms
5,376 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 1,512 ms
39,520 KB |
testcase_34 | AC | 1,421 ms
39,504 KB |
testcase_35 | AC | 1,486 ms
39,508 KB |
testcase_36 | AC | 1,444 ms
39,492 KB |
testcase_37 | AC | 1,492 ms
39,644 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; }