結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | roaris |
提出日時 | 2020-11-28 20:16:09 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,023 ms / 3,000 ms |
コード長 | 2,062 bytes |
コンパイル時間 | 583 ms |
コンパイル使用メモリ | 82,272 KB |
実行使用メモリ | 185,876 KB |
最終ジャッジ日時 | 2024-09-13 00:06:44 |
合計ジャッジ時間 | 31,207 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,456 KB |
testcase_01 | AC | 40 ms
55,036 KB |
testcase_02 | AC | 857 ms
175,516 KB |
testcase_03 | AC | 788 ms
163,852 KB |
testcase_04 | AC | 943 ms
180,220 KB |
testcase_05 | AC | 795 ms
177,468 KB |
testcase_06 | AC | 896 ms
172,584 KB |
testcase_07 | AC | 864 ms
172,932 KB |
testcase_08 | AC | 783 ms
165,408 KB |
testcase_09 | AC | 856 ms
166,552 KB |
testcase_10 | AC | 816 ms
165,300 KB |
testcase_11 | AC | 1,008 ms
174,916 KB |
testcase_12 | AC | 965 ms
176,344 KB |
testcase_13 | AC | 884 ms
176,972 KB |
testcase_14 | AC | 833 ms
167,020 KB |
testcase_15 | AC | 850 ms
168,288 KB |
testcase_16 | AC | 955 ms
180,692 KB |
testcase_17 | AC | 942 ms
179,104 KB |
testcase_18 | AC | 908 ms
169,212 KB |
testcase_19 | AC | 912 ms
172,432 KB |
testcase_20 | AC | 897 ms
170,872 KB |
testcase_21 | AC | 924 ms
176,740 KB |
testcase_22 | AC | 937 ms
173,452 KB |
testcase_23 | AC | 900 ms
177,444 KB |
testcase_24 | AC | 908 ms
171,440 KB |
testcase_25 | AC | 997 ms
180,732 KB |
testcase_26 | AC | 914 ms
173,416 KB |
testcase_27 | AC | 927 ms
174,620 KB |
testcase_28 | AC | 845 ms
173,236 KB |
testcase_29 | AC | 936 ms
178,948 KB |
testcase_30 | AC | 1,002 ms
179,316 KB |
testcase_31 | AC | 975 ms
178,936 KB |
testcase_32 | AC | 39 ms
53,948 KB |
testcase_33 | AC | 433 ms
169,876 KB |
testcase_34 | AC | 1,023 ms
185,876 KB |
ソースコード
import sys input = sys.stdin.readline from heapq import * class Mincostflow: def __init__(self, N): self.N = N self.G = [[] for _ 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 min_cost_flow(self, s, t, f): res = 0 h = [0]*self.N while f>0: dist = [10**18]*self.N dist[s] = 0 pq = [(0, s)] prev_v = [-1]*self.N prev_e = [-1]*self.N while pq: d, v = heappop(pq) if dist[v]<d: continue for i in range(len(self.G[v])): nv, cap, cost, _ = self.G[v][i] if cap>0 and dist[nv]>dist[v]+cost+h[v]-h[nv]: dist[nv] = dist[v]+cost+h[v]-h[nv] prev_v[nv] = v prev_e[nv] = i heappush(pq, (dist[nv], nv)) if dist[t]==10**18: return -1 for i in range(N): h[i] += dist[i] d = f v = t while v!=s: d = min(d, self.G[prev_v[v]][prev_e[v]][1]) v = prev_v[v] f -= d res += d*h[t] v = t while v!=s: self.G[prev_v[v]][prev_e[v]][1] -= d rev = self.G[prev_v[v]][prev_e[v]][3] self.G[v][rev][1] += d v = prev_v[v] return res N, M = map(int, input().split()) mcf = Mincostflow(N) for _ in range(M): u, v, c, d = map(int, input().split()) mcf.add_edge(u-1, v-1, 1, c) mcf.add_edge(v-1, u-1, 1, c) mcf.add_edge(u-1, v-1, 1, d) mcf.add_edge(v-1, u-1, 1, d) print(mcf.min_cost_flow(0, N-1, 2))