結果
問題 | No.2604 Initial Motion |
ユーザー | tnakao0123 |
提出日時 | 2024-06-18 10:08:42 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,331 bytes |
コンパイル時間 | 1,302 ms |
コンパイル使用メモリ | 73,444 KB |
実行使用メモリ | 140,956 KB |
最終ジャッジ日時 | 2024-06-18 10:08:57 |
合計ジャッジ時間 | 15,133 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
140,956 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 12 ms
6,944 KB |
testcase_04 | AC | 14 ms
6,940 KB |
testcase_05 | AC | 11 ms
6,940 KB |
testcase_06 | AC | 12 ms
6,944 KB |
testcase_07 | AC | 12 ms
6,940 KB |
testcase_08 | AC | 13 ms
6,940 KB |
testcase_09 | AC | 11 ms
6,940 KB |
testcase_10 | AC | 12 ms
6,940 KB |
testcase_11 | AC | 12 ms
6,944 KB |
testcase_12 | AC | 13 ms
6,944 KB |
testcase_13 | AC | 392 ms
6,940 KB |
testcase_14 | AC | 265 ms
6,940 KB |
testcase_15 | AC | 135 ms
6,944 KB |
testcase_16 | AC | 320 ms
6,944 KB |
testcase_17 | AC | 420 ms
6,940 KB |
testcase_18 | AC | 390 ms
6,940 KB |
testcase_19 | AC | 398 ms
6,940 KB |
testcase_20 | AC | 313 ms
6,940 KB |
testcase_21 | AC | 267 ms
6,940 KB |
testcase_22 | AC | 372 ms
6,944 KB |
testcase_23 | AC | 286 ms
6,944 KB |
testcase_24 | AC | 375 ms
6,940 KB |
testcase_25 | AC | 362 ms
6,944 KB |
testcase_26 | AC | 313 ms
6,940 KB |
testcase_27 | AC | 235 ms
6,940 KB |
testcase_28 | AC | 324 ms
6,940 KB |
testcase_29 | AC | 346 ms
6,940 KB |
testcase_30 | AC | 251 ms
6,940 KB |
testcase_31 | AC | 305 ms
6,940 KB |
testcase_32 | AC | 280 ms
6,944 KB |
testcase_33 | AC | 130 ms
6,944 KB |
testcase_34 | AC | 79 ms
6,940 KB |
testcase_35 | AC | 313 ms
6,944 KB |
testcase_36 | AC | 448 ms
6,940 KB |
testcase_37 | AC | 173 ms
6,944 KB |
testcase_38 | AC | 3 ms
6,944 KB |
testcase_39 | TLE | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
ソースコード
/* -*- coding: utf-8 -*- * * 2604.cc: No.2604 Initial Motion - yukicoder */ #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<utility> using namespace std; /* constant */ const int MAX_K = 2000; const int MAX_N = 2000; const int INF = 1 << 30; const long long LINF = 1LL << 62; /* typedef */ using ll = long long; template <typename T> struct MinCostFlow { typedef pair<int,int> pii; typedef pair<T,int> pti; typedef pair<T,T> ptt; typedef vector<pii> vpii; typedef vector<vpii> vvpii; int n, m; vvpii nbrs; T inf; T max_flow, min_cost; vpii prvs; vector<T> minfs, caps, costs, flows, ds; MinCostFlow(int _n, T _inf): n(_n), m(0), inf(_inf) { nbrs.assign(n, vpii()); minfs.assign(n, 0); prvs.assign(n, pii(-1, -1)); caps.clear(), costs.clear(), flows.clear(); ds.assign(n, 0); max_flow = min_cost = 0; } int addedges(int u, int v, T cap, T cost) { // edge[m]: u -> v, cap=c nbrs[u].push_back(pii(v, m)); caps.push_back(cap); costs.push_back(cost); flows.push_back(0); // edge[m + 1]: v -> u, cap=0 nbrs[v].push_back(pii(u, m + 1)); caps.push_back(0); costs.push_back(-cost); flows.push_back(0); m += 2; return m; } ptt flow(int st, int gl) { // return ptt(max_flow, min_cost) return flow(st, gl, inf, false); } ptt flow(int st, int gl, T limit, bool cont) { // return ptt(max_flow, min_cost) if (! cont) { fill(flows.begin(), flows.end(), 0); max_flow = min_cost = 0; } while (max_flow < limit) { //printf("max_flow = %d, limit = %d\n", max_flow, limit); fill(prvs.begin(), prvs.end(), pii(-1, -1)); fill(ds.begin(), ds.end(), inf); prvs[st] = pii(st, -1); minfs[st] = inf; ds[st] = 0; priority_queue<pti> q; q.push(pti(0, st)); while (! q.empty()) { pti u = q.top(); q.pop(); T ud = -u.first; int ui = u.second; if (ud != ds[ui]) continue; if (ui == gl) break; for (auto ve: nbrs[ui]) { int vi = ve.first, ei = ve.second; T vc = caps[ei] - flows[ei]; T vd = ud + costs[ei]; if (vc > 0 && ds[vi] > vd) { prvs[vi] = pii(ui, ei); minfs[vi] = (minfs[ui] < vc) ? minfs[ui] : vc; ds[vi] = vd; q.push(pti(-vd, vi)); } } } if (prvs[gl].first < 0) break; T min_flow = min(minfs[gl], limit - max_flow); for (int j = gl; j != st;) { int i = prvs[j].first, ei = prvs[j].second; flows[ei] += min_flow; flows[ei ^ 1] -= min_flow; j = i; } max_flow += min_flow; min_cost += ds[gl] * min_flow; } return ptt(max_flow, min_cost); } }; /* global variables */ int as[MAX_K], bs[MAX_N]; /* subroutines */ /* main */ int main() { int k, n, m; scanf("%d%d%d", &k, &n, &m); for (int i = 0; i < k; i++) scanf("%d", as + i), as[i]--; for (int i = 0; i < n; i++) scanf("%d", bs + i); MinCostFlow<ll> mcf(n + 2, LINF); for (int i = 0; i < m; i++) { int u, v; ll d; scanf("%d%d%lld", &u, &v, &d); u--, v--; mcf.addedges(u, v, INF, d); mcf.addedges(v, u, INF, d); } for (int i = 0; i < k; i++) mcf.addedges(n, as[i], 1, 0); for (int i = 0; i < n; i++) mcf.addedges(i, n + 1, bs[i], 0); auto f = mcf.flow(n, n + 1); printf("%lld\n", f.second); return 0; }