結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | Kite_kuma |
提出日時 | 2020-10-30 22:26:41 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,743 ms / 3,000 ms |
コード長 | 4,353 bytes |
コンパイル時間 | 440 ms |
コンパイル使用メモリ | 82,288 KB |
実行使用メモリ | 279,592 KB |
最終ジャッジ日時 | 2024-09-13 00:27:03 |
合計ジャッジ時間 | 45,984 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
55,620 KB |
testcase_01 | AC | 42 ms
53,488 KB |
testcase_02 | AC | 1,394 ms
267,096 KB |
testcase_03 | AC | 1,043 ms
247,384 KB |
testcase_04 | AC | 1,661 ms
275,444 KB |
testcase_05 | AC | 1,361 ms
269,008 KB |
testcase_06 | AC | 1,604 ms
268,764 KB |
testcase_07 | AC | 1,482 ms
269,180 KB |
testcase_08 | AC | 1,210 ms
252,032 KB |
testcase_09 | AC | 1,007 ms
252,580 KB |
testcase_10 | AC | 1,024 ms
248,836 KB |
testcase_11 | AC | 1,415 ms
272,520 KB |
testcase_12 | AC | 1,404 ms
273,576 KB |
testcase_13 | AC | 1,235 ms
267,220 KB |
testcase_14 | AC | 1,489 ms
253,580 KB |
testcase_15 | AC | 1,029 ms
252,836 KB |
testcase_16 | AC | 1,587 ms
275,800 KB |
testcase_17 | AC | 1,537 ms
270,896 KB |
testcase_18 | AC | 1,352 ms
262,580 KB |
testcase_19 | AC | 1,207 ms
268,476 KB |
testcase_20 | AC | 1,404 ms
266,880 KB |
testcase_21 | AC | 1,493 ms
267,564 KB |
testcase_22 | AC | 1,597 ms
272,624 KB |
testcase_23 | AC | 1,236 ms
267,504 KB |
testcase_24 | AC | 1,531 ms
268,628 KB |
testcase_25 | AC | 1,487 ms
273,876 KB |
testcase_26 | AC | 1,413 ms
268,896 KB |
testcase_27 | AC | 1,254 ms
271,072 KB |
testcase_28 | AC | 1,233 ms
267,700 KB |
testcase_29 | AC | 1,743 ms
274,760 KB |
testcase_30 | AC | 1,340 ms
272,568 KB |
testcase_31 | AC | 1,493 ms
272,192 KB |
testcase_32 | AC | 39 ms
55,084 KB |
testcase_33 | AC | 863 ms
267,600 KB |
testcase_34 | AC | 1,443 ms
279,592 KB |
ソースコード
from heapq import heappop, heappush, heapify import sys input = sys.stdin.readline # N, K = map(int, input().split()) # a = [list(map(int, input().split())) for _ in range(N)] 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): 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): 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): return self.flow_with_limit(s, t, 2**63 - 1) def flow_with_limit( self, s, t, limit): return self.slope_with_limit(s, t, limit)[-1] def slope(self, s, t): return self.slope_with_limit(s, t, 2**63 - 1) def slope_with_limit(self, s, t, limit): 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 inf = 10 ** 10 # mcf = MinCostFlow(2 * N + 2) # mcf.add_edge(2 * N, 2 * N + 1, N * K, inf) # for i in range(N): # mcf.add_edge(2 * N, i, K, 0) # mcf.add_edge(N + i, 2 * N + 1, K, 0) # for j in range(N): # mcf.add_edge(i, N + j, 1, inf - a[i][j]) # print(-mcf.flow_with_limit(2 * N, 2 * N + 1, N * K)[1] + N * K * inf) # res = [["."] * N for _ in range(N)] # for f, t, _, flow, _ in mcf.edges(): # if flow == 0 or max(f, t) >= 2 * N: # continue # res[f][t - N] = "X" # for r in res: # print("".join(r)) def main() -> None: n, m = map(int, input().split()) s, t = 0, n - 1 # graph = mcf_graph(n + m) graph = MinCostFlow(n + m) for i in range(m): u, v, c, d = map(int, input().split()) assert 1 <= u <= n and 1 <= v <= n and c <= d u -= 1 v -= 1 mid = n + i graph.add_edge(u, mid, 2, c) graph.add_edge(mid, v, 1, 0) graph.add_edge(mid, v, 1, d - c) graph.add_edge(v, mid, 2, c) graph.add_edge(mid, u, 1, 0) graph.add_edge(mid, u, 1, d - c) # flow, cost = graph.flow(s, t, 2) flow, cost = graph.flow_with_limit(s, t, 2) assert flow == 2 print(cost) if __name__ == '__main__': main()