結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | ああいい |
提出日時 | 2022-04-14 08:40:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,225 ms / 3,000 ms |
コード長 | 2,218 bytes |
コンパイル時間 | 938 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 230,224 KB |
最終ジャッジ日時 | 2024-06-06 13:35:24 |
合計ジャッジ時間 | 38,035 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 33 ms
52,736 KB |
testcase_01 | AC | 36 ms
52,608 KB |
testcase_02 | AC | 1,080 ms
218,684 KB |
testcase_03 | AC | 952 ms
202,508 KB |
testcase_04 | AC | 1,153 ms
227,424 KB |
testcase_05 | AC | 1,035 ms
221,420 KB |
testcase_06 | AC | 1,032 ms
213,112 KB |
testcase_07 | AC | 1,064 ms
212,532 KB |
testcase_08 | AC | 996 ms
203,876 KB |
testcase_09 | AC | 958 ms
207,536 KB |
testcase_10 | AC | 958 ms
203,924 KB |
testcase_11 | AC | 1,225 ms
220,232 KB |
testcase_12 | AC | 1,102 ms
220,268 KB |
testcase_13 | AC | 1,056 ms
220,524 KB |
testcase_14 | AC | 998 ms
205,616 KB |
testcase_15 | AC | 1,022 ms
206,588 KB |
testcase_16 | AC | 1,163 ms
227,704 KB |
testcase_17 | AC | 1,087 ms
223,116 KB |
testcase_18 | AC | 1,150 ms
208,200 KB |
testcase_19 | AC | 1,071 ms
213,896 KB |
testcase_20 | AC | 1,009 ms
211,952 KB |
testcase_21 | AC | 1,032 ms
219,608 KB |
testcase_22 | AC | 1,128 ms
216,484 KB |
testcase_23 | AC | 1,076 ms
220,268 KB |
testcase_24 | AC | 1,070 ms
214,236 KB |
testcase_25 | AC | 1,143 ms
226,456 KB |
testcase_26 | AC | 1,047 ms
212,852 KB |
testcase_27 | AC | 1,095 ms
219,292 KB |
testcase_28 | AC | 950 ms
212,512 KB |
testcase_29 | AC | 1,109 ms
226,804 KB |
testcase_30 | AC | 1,176 ms
224,712 KB |
testcase_31 | AC | 1,120 ms
224,444 KB |
testcase_32 | AC | 33 ms
52,992 KB |
testcase_33 | AC | 519 ms
214,720 KB |
testcase_34 | AC | 1,206 ms
230,224 KB |
ソースコード
N,M = map(int,input().split()) #その2 最初はコストが全部正の場合に使えるダイクストラ from heapq import heappush,heappop class MinCostFlow: inf = 10 ** 18 def __init__(self,N): self.N = N self.G = [[] for _ in range(N)] self.H = [0] * N self.edge = [] def add_edge(self,fr,to,cap,cost): e = [to,cap,cost,None] r = e[3] = [fr,0,-cost,e] self.G[fr].append(e) self.G[to].append(r) self.edge.append(e) def get_edge(self,i): return self.edge[i] def flow(self,s,t,f): N = self.N G = self.G inf = MinCostFlow.inf res = 0 H = self.H #ポテンシャル、コストが最初は正なので、最初は0でいい prev_v = [0] * N prev_e = [None] * N d0 = [inf] * N dist = [inf] * N while f: dist[:] = d0 dist[s] = 0 q = [(0,s)] while q: c,v = heappop(q) if dist[v] < c:continue r0 = dist[v] + H[v] for e in G[v]: w,cap,cost,_ = e if cap > 0 and r0 + cost - H[w] < dist[w]: dist[w] = r = r0 + cost - H[w] prev_v[w] = v prev_e[w] = e heappush(q,(r,w)) if dist[t] == inf: return None """ for i in range(N): H[i] += dist[i] """ H = [h + d for h,d in zip(H,dist)] d = f v = t while v != s: d = min(d,prev_e[v][1]) v = prev_v[v] f -= d res += d * H[t] v = t while v != s: e = prev_e[v] e[1] -= d e[3][1] += d v = prev_v[v] return res mincost = MinCostFlow(N) for _ in range(M): u,v,c,d = map(int,input().split()) u -= 1 v -= 1 mincost.add_edge(u,v,1,c) mincost.add_edge(u,v,1,d) mincost.add_edge(v,u,1,c) mincost.add_edge(v,u,1,d) print(mincost.flow(0,N-1,2))