結果

問題 No.654 Air E869120
ユーザー gr1msl3ygr1msl3y
提出日時 2022-02-23 18:21:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 3,133 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 80,400 KB
最終ジャッジ日時 2023-09-14 11:16:53
合計ジャッジ時間 7,913 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
72,096 KB
testcase_01 AC 93 ms
72,180 KB
testcase_02 AC 88 ms
71,424 KB
testcase_03 AC 90 ms
71,492 KB
testcase_04 AC 91 ms
72,156 KB
testcase_05 AC 91 ms
72,260 KB
testcase_06 AC 91 ms
72,288 KB
testcase_07 AC 91 ms
72,124 KB
testcase_08 AC 89 ms
71,532 KB
testcase_09 AC 90 ms
72,244 KB
testcase_10 AC 165 ms
78,616 KB
testcase_11 AC 156 ms
78,416 KB
testcase_12 AC 157 ms
78,460 KB
testcase_13 AC 164 ms
78,760 KB
testcase_14 AC 161 ms
78,936 KB
testcase_15 AC 157 ms
78,724 KB
testcase_16 AC 187 ms
80,080 KB
testcase_17 AC 183 ms
79,568 KB
testcase_18 AC 181 ms
80,028 KB
testcase_19 AC 193 ms
80,400 KB
testcase_20 AC 172 ms
79,448 KB
testcase_21 AC 169 ms
79,432 KB
testcase_22 AC 160 ms
79,472 KB
testcase_23 AC 167 ms
79,192 KB
testcase_24 AC 177 ms
79,876 KB
testcase_25 AC 162 ms
79,384 KB
testcase_26 AC 165 ms
79,320 KB
testcase_27 AC 150 ms
79,476 KB
testcase_28 AC 159 ms
79,596 KB
testcase_29 AC 151 ms
79,380 KB
testcase_30 AC 133 ms
78,332 KB
testcase_31 AC 138 ms
78,352 KB
testcase_32 AC 132 ms
77,728 KB
testcase_33 AC 132 ms
77,752 KB
testcase_34 AC 134 ms
78,528 KB
testcase_35 AC 87 ms
71,224 KB
testcase_36 AC 90 ms
71,484 KB
testcase_37 AC 88 ms
71,260 KB
testcase_38 AC 91 ms
71,232 KB
testcase_39 AC 92 ms
72,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque, defaultdict


class Dinic:
    """
    Usage:
       mf = Dinic(n)
    -> mf.add_link(from, to, capacity)
    -> mf.max_flow(source, target)
    """

    def __init__(self, n):
        self.n = n
        self.links = [[] for _ in range(n)]

    def add_link(self, from_, to, capacity):
        self.links[from_].append([capacity, to, len(self.links[to])])
        self.links[to].append([0, from_, len(self.links[from_]) - 1])

    def bfs(self, s):
        depth = [-1] * self.n
        depth[s] = 0
        q = deque([s])
        while q:
            v = q.popleft()
            for cap, to, rev in self.links[v]:
                if cap > 0 and depth[to] < 0:
                    depth[to] = depth[v] + 1
                    q.append(to)
        return depth

    def dfs(self, s, t, depth, progress, link_counts):
        links = self.links
        stack = [s]

        while stack:
            v = stack[-1]
            if v == t:
                break
            for i in range(progress[v], link_counts[v]):
                progress[v] = i
                cap, to, rev = links[v][i]
                if cap == 0 or depth[v] >= depth[to] or progress[to] >= link_counts[to]:
                    continue
                stack.append(to)
                break
            else:
                progress[v] += 1
                stack.pop()
        else:
            return 0

        f = 1 << 60
        fwd_links = []
        bwd_links = []
        for v in stack[:-1]:
            cap, to, rev = link = links[v][progress[v]]
            f = min(f, cap)
            fwd_links.append(link)
            bwd_links.append(links[to][rev])

        for link in fwd_links:
            link[0] -= f

        for link in bwd_links:
            link[0] += f

        return f

    def max_flow(self, s, t):
        link_counts = list(map(len, self.links))
        flow = 0
        while True:
            depth = self.bfs(s)
            if depth[t] < 0:
                break
            progress = [0] * self.n
            current_flow = self.dfs(s, t, depth, progress, link_counts)
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, depth, progress, link_counts)
        return flow


N, M, d = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(M)]
B = [set() for _ in range(N+1)]
ct = 0
for u, v, p, q, w in A:
    B[u].add((p, 1))
    B[v].add((q+d, 0))

C = [{} for _ in range(N+1)]
for i, b in enumerate(B):
    B[i] = list(B[i])
    if not B[i]:
        continue
    B[i].sort()
    for t, f in B[i]:
        C[i][(t, f)] = ct
        ct += 1

mf_flow = Dinic(ct+2)
for u, v, p, q, w in A:
    inds = C[u][(p, 1)]
    indt = C[v][(q+d, 0)]
    mf_flow.add_link(inds, indt, w)

INF = 10**18
for i in range(N+1):
    for j in range(len(B[i])-1):
        mf_flow.add_link(C[i][tuple(B[i][j])], C[i][tuple(B[i][j+1])], INF)

for b in B[1]:
    mf_flow.add_link(ct, C[1][b], INF)
for b in B[N]:
    if b[1] == 0 and b[0] <= 10**9+d:
        mf_flow.add_link(C[N][b], ct+1, INF)

print(mf_flow.max_flow(ct, ct+1))
0