結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | None |
提出日時 | 2021-04-18 20:10:28 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,076 ms / 3,000 ms |
コード長 | 3,996 bytes |
コンパイル時間 | 643 ms |
コンパイル使用メモリ | 82,376 KB |
実行使用メモリ | 208,888 KB |
最終ジャッジ日時 | 2024-07-04 04:50:13 |
合計ジャッジ時間 | 29,757 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
53,120 KB |
testcase_01 | AC | 41 ms
53,376 KB |
testcase_02 | AC | 874 ms
199,984 KB |
testcase_03 | AC | 765 ms
183,960 KB |
testcase_04 | AC | 978 ms
206,868 KB |
testcase_05 | AC | 867 ms
201,420 KB |
testcase_06 | AC | 1,000 ms
194,288 KB |
testcase_07 | AC | 882 ms
194,680 KB |
testcase_08 | AC | 823 ms
185,192 KB |
testcase_09 | AC | 745 ms
187,792 KB |
testcase_10 | AC | 750 ms
184,788 KB |
testcase_11 | AC | 866 ms
199,572 KB |
testcase_12 | AC | 864 ms
200,460 KB |
testcase_13 | AC | 757 ms
199,516 KB |
testcase_14 | AC | 958 ms
187,948 KB |
testcase_15 | AC | 767 ms
188,228 KB |
testcase_16 | AC | 950 ms
207,216 KB |
testcase_17 | AC | 943 ms
204,468 KB |
testcase_18 | AC | 902 ms
190,384 KB |
testcase_19 | AC | 781 ms
193,684 KB |
testcase_20 | AC | 831 ms
193,804 KB |
testcase_21 | AC | 824 ms
200,424 KB |
testcase_22 | AC | 952 ms
196,836 KB |
testcase_23 | AC | 797 ms
201,792 KB |
testcase_24 | AC | 988 ms
194,712 KB |
testcase_25 | AC | 922 ms
205,396 KB |
testcase_26 | AC | 849 ms
194,584 KB |
testcase_27 | AC | 823 ms
198,608 KB |
testcase_28 | AC | 825 ms
193,840 KB |
testcase_29 | AC | 1,076 ms
207,716 KB |
testcase_30 | AC | 822 ms
203,308 KB |
testcase_31 | AC | 937 ms
204,468 KB |
testcase_32 | AC | 39 ms
53,376 KB |
testcase_33 | AC | 515 ms
194,772 KB |
testcase_34 | AC | 815 ms
208,888 KB |
ソースコード
class MinCostFlow(): def __init__(self, n): self.n = n self.graph = [[] for _ in range(n)] self.pos = [] def add_edge(self, fr, to, cap, cost): # assert 0 <= fr < self.n # assert 0 <= to < self.n m = len(self.pos) self.pos.append((fr, len(self.graph[fr]))) self.graph[fr].append([to, len(self.graph[to]), cap, cost]) self.graph[to].append([fr, len(self.graph[fr]) - 1, 0, -cost]) return m def get_edge(self, idx): """ i 番目に追加された辺について (from, to, 初期の cap, 現在の流量(始めは 0), コスト) """ # assert 0 <= idx < len(self.pos) to, rev, cap, cost = self.graph[self.pos[idx][0]][self.pos[idx][1]] rev_to, rev_rev, rev_cap, rev_cost = self.graph[to][rev] return self.pos[idx][0], to, cap + rev_cap, rev_cap, cost def edges(self): for i in range(len(self.pos)): yield self.get_edge(i) def dual_ref(self, s, t): dist = [2 ** 63 - 1] * self.n dist[s] = 0 vis = [0] * self.n self.pv = [-1] * self.n self.pe = [-1] * self.n queue = [] heappush(queue, (0, s)) while queue: k, v = heappop(queue) if vis[v]: continue vis[v] = True if v == t: break for i in range(len(self.graph[v])): to, rev, cap, cost = self.graph[v][i] if vis[to] or cap == 0: continue cost += self.dual[v] - self.dual[to] if dist[to] - dist[v] > cost: dist[to] = dist[v] + cost self.pv[to] = v self.pe[to] = i heappush(queue, (dist[to], to)) if not vis[t]: return False for v in range(self.n): if not vis[v]: continue self.dual[v] -= dist[t] - dist[v] return True def flow(self, s, t, limit=2**63-1): """ return: (最大流量、最小コスト) """ return self.slope(s, t, limit)[-1] def slope(self, s, t, limit=2**63-1): """ 各流量毎の最小コスト (流量でソートされている) return: (流量1、最小コスト1), (流量2、最小コスト2), (流量3、最小コスト3),... """ # assert 0 <= s < self.n # assert 0 <= t < self.n # assert s != t flow = 0 cost = 0 prev_cost = -1 res = [(flow, cost)] self.dual = [0] * self.n while flow < limit: if not self.dual_ref(s, t): break c = limit - flow v = t while v != s: c = min(c, self.graph[self.pv[v]][self.pe[v]][2]) v = self.pv[v] v = t while v != s: to, rev, cap, _ = self.graph[self.pv[v]][self.pe[v]] self.graph[self.pv[v]][self.pe[v]][2] -= c self.graph[v][rev][2] += c v = self.pv[v] d = -self.dual[s] flow += c cost += c * d if prev_cost == d: res.pop() res.append((flow, cost)) prev_cost = cost return res def example(): global input example = iter( """ 4 5 2 0 1 2 1 0 2 1 2 1 2 1 1 1 3 1 3 2 3 2 1 """ .strip().split("\n")) input = lambda: next(example) ############################################################ from heapq import * import sys sys.setrecursionlimit(10**9) input = sys.stdin.readline # example() BIG = 10**18 N,M = map(int,input().split()) # F=sinkに到達させたい流量 F=2 mcf = MinCostFlow(N) for _ in range(M): u, v, cost1, cost2 = map(int, input().split()) u,v=u-1,v-1 mcf.add_edge(u, v, 1, cost1) mcf.add_edge(u, v, 1, cost2) mcf.add_edge(v, u, 1, cost1) mcf.add_edge(v, u, 1, cost2) f,cost=mcf.flow(0,N-1,F) print(cost)