結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | rlangevin |
提出日時 | 2023-10-20 00:07:23 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,243 bytes |
コンパイル時間 | 137 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 188,832 KB |
最終ジャッジ日時 | 2024-09-19 20:02:23 |
合計ジャッジ時間 | 83,608 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
57,984 KB |
testcase_01 | AC | 38 ms
52,096 KB |
testcase_02 | AC | 2,405 ms
182,400 KB |
testcase_03 | AC | 2,615 ms
170,720 KB |
testcase_04 | TLE | - |
testcase_05 | AC | 2,165 ms
185,600 KB |
testcase_06 | TLE | - |
testcase_07 | TLE | - |
testcase_08 | AC | 1,795 ms
172,032 KB |
testcase_09 | TLE | - |
testcase_10 | AC | 1,745 ms
171,292 KB |
testcase_11 | AC | 2,975 ms
181,760 KB |
testcase_12 | AC | 2,751 ms
182,272 KB |
testcase_13 | AC | 2,719 ms
183,764 KB |
testcase_14 | AC | 2,297 ms
172,800 KB |
testcase_15 | AC | 2,530 ms
174,464 KB |
testcase_16 | AC | 2,648 ms
188,544 KB |
testcase_17 | TLE | - |
testcase_18 | AC | 2,931 ms
176,256 KB |
testcase_19 | AC | 2,490 ms
179,456 KB |
testcase_20 | AC | 2,867 ms
178,176 KB |
testcase_21 | TLE | - |
testcase_22 | TLE | - |
testcase_23 | TLE | - |
testcase_24 | AC | 2,553 ms
179,840 KB |
testcase_25 | AC | 2,723 ms
187,008 KB |
testcase_26 | TLE | - |
testcase_27 | AC | 2,568 ms
181,248 KB |
testcase_28 | AC | 2,455 ms
179,712 KB |
testcase_29 | TLE | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
ソースコード
import sys input = sys.stdin.readline class MinCostFlow: def __init__(self, N): self.N = N self.inf = 10 ** 18 self.G = [[] for i in range(N)] def add_edge(self, u, v, cap, cost): self.G[u].append((v, cap, cost, len(self.G[v]))) self.G[v].append((u, 0, -cost, len(self.G[u]) - 1)) def bellman_ford(self, s): dist = [self.inf] * self.N dist[s] = 0 pv = [0] * self.N pe = [0] * self.N while True: update = False for v in range(self.N): if dist[v] == self.inf: continue for i in range(len(self.G[v])): next, cap, cost, _ = self.G[v][i] if cap > 0 and dist[next] > dist[v] + cost: dist[next] = dist[v] + cost update = True pv[next] = v pe[next] = i if not update: break return dist, pv, pe def calc_min_cost_flow(self, s, t, f): result = 0 while f > 0: dist, pv, pe = self.bellman_ford(s) if dist[t] == self.inf: return self.inf flow = f v = t while v != s: flow = min(flow, self.G[pv[v]][pe[v]][1]) v = pv[v] result += flow * dist[t] f -= flow v = t while v != s: d, cap, cost, r = self.G[pv[v]][pe[v]] cap -= flow self.G[pv[v]][pe[v]] = (d, cap, cost, r) rev = self.G[pv[v]][pe[v]][3] d, cap, cost, r = self.G[v][rev] cap += flow self.G[v][rev] = (d, cap, cost, r) v = pv[v] return result N, M = map(int, input().split()) G = MinCostFlow(N) for i in range(M): u, v, c, d = map(int, input().split()) u, v = u - 1, v - 1 G.add_edge(u, v, 1, c) G.add_edge(u, v, 1, d) G.add_edge(v, u, 1, c) G.add_edge(v, u, 1, d) print(G.calc_min_cost_flow(0, N-1, 2))