結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | aaaaaaaaaa2230 |
提出日時 | 2021-03-09 11:12:09 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,569 bytes |
コンパイル時間 | 436 ms |
コンパイル使用メモリ | 82,244 KB |
実行使用メモリ | 150,788 KB |
最終ジャッジ日時 | 2024-10-11 01:48:02 |
合計ジャッジ時間 | 16,063 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,760 KB |
testcase_01 | AC | 39 ms
53,120 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | AC | 39 ms
53,248 KB |
testcase_33 | AC | 419 ms
144,112 KB |
testcase_34 | WA | - |
ソースコード
from heapq import heappush, heappop class mincostflow: class edge: def __init__(self, from_, to, cap, flow, cost): self.from_ = from_ self.to = to self.cap = cap self.flow = flow self.cost = cost class _edge: def __init__(self, to, rev, cap, cost): self.to = to self.rev = rev self.cap = cap self.cost = cost def __init__(self, n): self.n = n self.pos = [] self.g = [[] for i in range(n)] def add_edge(self, from_, to, cap, cost): m = len(self.pos) self.pos.append((from_, len(self.g[from_]))) self.g[from_].append(self.__class__._edge(to, len(self.g[to]), cap, cost)) self.g[to].append(self.__class__._edge(from_, len(self.g[from_])-1, 0, -cost)) return m def get_edge(self, i): _e = self.g[self.pos[i][0]][self.pos[i][1]] _re = self.g[_e.to][_e.rev] return self.edge(self.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap, _e.cost) def edges(self): result = [] for i in range(len(self.pos)): result.append(self.get_edge(i)) return result def slope(self, s, t, flow_limit=10**20, inf=10**20): dual = [0]*self.n dist = [inf]*self.n pv = [-1]*self.n pe = [-1]*self.n vis = [False]*self.n def _dual_ref(): nonlocal dual, dist, pv, pe, vis dist = [inf]*self.n pv = [-1]*self.n pe = [-1]*self.n vis = [False]*self.n que = [(0,s)] dist[s] = 0 while que: _,v = heappop(que) if vis[v]: continue vis[v] = True if v == t: break for i in range(len(self.g[v])): e = self.g[v][i] if vis[e.to] or e.cap == 0: continue cost = e.cost - dual[e.to] + dual[v] if dist[e.to] > dist[v] + cost: dist[e.to] = dist[v] + cost pv[e.to] = v pe[e.to] = i heappush(que, (dist[e.to],e.to)) if not vis[t]: return False for v in range(self.n): if not vis[v]: continue dual[v] -= dist[t] - dist[v] return True flow = 0 cost = 0 prev_cost = -1 result = [(flow, cost)] while flow < flow_limit: if not _dual_ref(): break c = flow_limit - flow v = t while v != s: c = min(c, self.g[pv[v]][pe[v]].cap) v = pv[v] v = t while v != s: e = self.g[pv[v]][pe[v]] e.cap -= c self.g[v][e.rev].cap += c v = pv[v] d = -dual[s] flow += c cost += c * d if prev_cost == d: result.pop() result.append((flow, cost)) prev_cost = cost return result def flow(self, s, t, flow_limit=10**20): return self.slope(s, t, flow_limit)[-1] n,m = map(int,input().split()) g = mincostflow(n+1) for i in range(m): u,v,c,d = map(int,input().split()) g.add_edge(u,v,1,c) g.add_edge(u,v,1,d) print(g.flow(1,n,2)[1])