結果
問題 | No.2604 Initial Motion |
ユーザー | rlangevin |
提出日時 | 2024-01-19 08:57:30 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,261 bytes |
コンパイル時間 | 222 ms |
コンパイル使用メモリ | 82,576 KB |
実行使用メモリ | 89,920 KB |
最終ジャッジ日時 | 2024-09-28 03:20:20 |
合計ジャッジ時間 | 7,514 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,304 KB |
testcase_01 | AC | 37 ms
53,512 KB |
testcase_02 | AC | 45 ms
60,884 KB |
testcase_03 | AC | 210 ms
76,528 KB |
testcase_04 | AC | 231 ms
76,716 KB |
testcase_05 | AC | 232 ms
76,956 KB |
testcase_06 | AC | 214 ms
76,644 KB |
testcase_07 | AC | 216 ms
76,912 KB |
testcase_08 | AC | 211 ms
77,264 KB |
testcase_09 | AC | 292 ms
76,704 KB |
testcase_10 | AC | 205 ms
76,616 KB |
testcase_11 | AC | 215 ms
76,624 KB |
testcase_12 | AC | 309 ms
77,132 KB |
testcase_13 | TLE | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
ソースコード
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 K, N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) G = MinCostFlow(N + 2) s, g = 0, N + 1 for i in range(M): u, v, d = map(int, input().split()) G.add_edge(u, v, K, d) G.add_edge(v, u, K, d) for i in range(K): G.add_edge(s, A[i], 1, 0) for i in range(N): G.add_edge(i + 1, g, B[i], 0) print(G.calc_min_cost_flow(s, g, K))