結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | nok0 |
提出日時 | 2020-10-30 22:24:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,935 ms / 3,000 ms |
コード長 | 2,005 bytes |
コンパイル時間 | 386 ms |
コンパイル使用メモリ | 82,228 KB |
実行使用メモリ | 261,320 KB |
最終ジャッジ日時 | 2024-09-13 00:25:56 |
合計ジャッジ時間 | 57,563 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,352 KB |
testcase_01 | AC | 40 ms
54,072 KB |
testcase_02 | AC | 1,753 ms
246,900 KB |
testcase_03 | AC | 1,587 ms
226,884 KB |
testcase_04 | AC | 1,856 ms
251,000 KB |
testcase_05 | AC | 1,789 ms
249,552 KB |
testcase_06 | AC | 1,734 ms
237,524 KB |
testcase_07 | AC | 1,791 ms
241,144 KB |
testcase_08 | AC | 1,602 ms
228,468 KB |
testcase_09 | AC | 1,631 ms
228,668 KB |
testcase_10 | AC | 1,574 ms
228,444 KB |
testcase_11 | AC | 1,805 ms
243,892 KB |
testcase_12 | AC | 1,763 ms
243,412 KB |
testcase_13 | AC | 1,784 ms
248,044 KB |
testcase_14 | AC | 1,626 ms
229,228 KB |
testcase_15 | AC | 1,682 ms
232,736 KB |
testcase_16 | AC | 1,827 ms
250,092 KB |
testcase_17 | AC | 1,854 ms
250,148 KB |
testcase_18 | AC | 1,729 ms
234,764 KB |
testcase_19 | AC | 1,720 ms
238,584 KB |
testcase_20 | AC | 1,662 ms
234,836 KB |
testcase_21 | AC | 1,779 ms
246,624 KB |
testcase_22 | AC | 1,660 ms
238,248 KB |
testcase_23 | AC | 1,841 ms
247,444 KB |
testcase_24 | AC | 1,709 ms
236,804 KB |
testcase_25 | AC | 1,838 ms
250,744 KB |
testcase_26 | AC | 1,696 ms
240,448 KB |
testcase_27 | AC | 1,771 ms
242,776 KB |
testcase_28 | AC | 1,646 ms
240,096 KB |
testcase_29 | AC | 1,739 ms
247,856 KB |
testcase_30 | AC | 1,866 ms
249,712 KB |
testcase_31 | AC | 1,854 ms
248,332 KB |
testcase_32 | AC | 40 ms
53,632 KB |
testcase_33 | AC | 826 ms
238,808 KB |
testcase_34 | AC | 1,935 ms
261,320 KB |
ソースコード
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappush, heappop class MinCostFlow: INF = 10**18 def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap, cost): G = self.G G[fr].append([to, cap, cost, len(G[to])]) G[to].append([fr, 0, -cost, len(G[fr])-1]) def flow(self, s, t, f): N = self.N; G = self.G INF = MinCostFlow.INF res = 0 H = [0]*N prv_v = [0]*N prv_e = [0]*N while f: dist = [INF]*N dist[s] = 0 que = [(0, s)] while que: c, v = heappop(que) if dist[v] < c: continue for i, (w, cap, cost, _) in enumerate(G[v]): if cap > 0 and dist[w] > dist[v] + cost + H[v] - H[w]: dist[w] = r = dist[v] + cost + H[v] - H[w] prv_v[w] = v; prv_e[w] = i heappush(que, (r, w)) if dist[t] == INF: return -1 for i in range(N): H[i] += dist[i] d = f; v = t while v != s: d = min(d, G[prv_v[v]][prv_e[v]][1]) v = prv_v[v] f -= d res += d * H[t] v = t while v != s: e = G[prv_v[v]][prv_e[v]] e[1] -= d G[v][e[3]][1] += d v = prv_v[v] return res n, m = map(int, input().split()) s, t, w = 0, n - 1, n G = MinCostFlow(n + 2 * m) def add(u, v, c, d): global w G.add_edge(u, w, 2, c) G.add_edge(w, v, 1, 0) G.add_edge(w, v, 1, d - c) w += 1 for i in range(m): u, v, c, d = map(int, input().split()) u -= 1 v -= 1 add(u, v, c, d) add(v, u, c, d) res = G.flow(s,t,2) print(res)