結果
問題 | No.904 サメトロ |
ユーザー | rlangevin |
提出日時 | 2023-01-24 12:30:48 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 83 ms / 1,000 ms |
コード長 | 2,000 bytes |
コンパイル時間 | 466 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 74,464 KB |
最終ジャッジ日時 | 2024-06-26 01:54:39 |
合計ジャッジ時間 | 3,489 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
54,016 KB |
testcase_01 | AC | 47 ms
54,016 KB |
testcase_02 | AC | 53 ms
61,056 KB |
testcase_03 | AC | 66 ms
66,432 KB |
testcase_04 | AC | 59 ms
62,720 KB |
testcase_05 | AC | 47 ms
53,888 KB |
testcase_06 | AC | 60 ms
64,000 KB |
testcase_07 | AC | 64 ms
66,176 KB |
testcase_08 | AC | 63 ms
65,280 KB |
testcase_09 | AC | 47 ms
54,144 KB |
testcase_10 | AC | 81 ms
72,448 KB |
testcase_11 | AC | 76 ms
69,888 KB |
testcase_12 | AC | 53 ms
60,672 KB |
testcase_13 | AC | 50 ms
59,520 KB |
testcase_14 | AC | 60 ms
63,872 KB |
testcase_15 | AC | 59 ms
63,872 KB |
testcase_16 | AC | 83 ms
74,112 KB |
testcase_17 | AC | 47 ms
54,400 KB |
testcase_18 | AC | 47 ms
53,888 KB |
testcase_19 | AC | 57 ms
61,952 KB |
testcase_20 | AC | 46 ms
55,844 KB |
testcase_21 | AC | 66 ms
69,008 KB |
testcase_22 | AC | 62 ms
65,184 KB |
testcase_23 | AC | 71 ms
69,448 KB |
testcase_24 | AC | 53 ms
63,436 KB |
testcase_25 | AC | 44 ms
55,400 KB |
testcase_26 | AC | 82 ms
74,464 KB |
testcase_27 | AC | 59 ms
65,312 KB |
testcase_28 | AC | 44 ms
55,444 KB |
testcase_29 | AC | 49 ms
62,112 KB |
testcase_30 | AC | 43 ms
55,580 KB |
testcase_31 | AC | 44 ms
55,388 KB |
testcase_32 | AC | 44 ms
54,168 KB |
testcase_33 | AC | 44 ms
54,476 KB |
testcase_34 | AC | 44 ms
54,804 KB |
testcase_35 | AC | 44 ms
54,116 KB |
ソースコード
# Dinic's algorithm # https://tjkendev.github.io/procon-library/python/max_flow/dinic.html from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow N = int(input()) G = Dinic(2 * N) for i in range(N - 1): A, B = map(int, input().split()) G.add_edge(0, i + 1, A) G.add_edge(i + N, 2 * N - 1, B) inf = 10 ** 18 for i in range(N - 1): for j in range(N - 1): if i == j: continue G.add_edge(i + 1, j + N, inf) print(G.flow(0, 2 * N - 1) + 1)