結果

問題 No.654 Air E869120
ユーザー gr1msl3ygr1msl3y
提出日時 2022-02-23 18:21:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 3,133 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 82,784 KB
実行使用メモリ 78,976 KB
最終ジャッジ日時 2024-07-01 18:46:10
合計ジャッジ時間 5,933 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
54,784 KB
testcase_01 AC 49 ms
54,528 KB
testcase_02 AC 48 ms
54,528 KB
testcase_03 AC 48 ms
54,400 KB
testcase_04 AC 50 ms
54,528 KB
testcase_05 AC 50 ms
54,272 KB
testcase_06 AC 49 ms
55,040 KB
testcase_07 AC 50 ms
54,784 KB
testcase_08 AC 48 ms
54,656 KB
testcase_09 AC 48 ms
54,784 KB
testcase_10 AC 137 ms
77,696 KB
testcase_11 AC 122 ms
77,184 KB
testcase_12 AC 130 ms
77,952 KB
testcase_13 AC 133 ms
78,208 KB
testcase_14 AC 129 ms
78,080 KB
testcase_15 AC 130 ms
77,184 KB
testcase_16 AC 162 ms
78,592 KB
testcase_17 AC 161 ms
78,976 KB
testcase_18 AC 157 ms
78,792 KB
testcase_19 AC 165 ms
78,708 KB
testcase_20 AC 147 ms
78,464 KB
testcase_21 AC 142 ms
78,976 KB
testcase_22 AC 134 ms
78,208 KB
testcase_23 AC 139 ms
78,592 KB
testcase_24 AC 150 ms
78,880 KB
testcase_25 AC 133 ms
78,208 KB
testcase_26 AC 138 ms
78,752 KB
testcase_27 AC 124 ms
78,464 KB
testcase_28 AC 134 ms
78,592 KB
testcase_29 AC 126 ms
78,336 KB
testcase_30 AC 107 ms
77,528 KB
testcase_31 AC 113 ms
77,824 KB
testcase_32 AC 104 ms
77,696 KB
testcase_33 AC 110 ms
77,312 KB
testcase_34 AC 109 ms
77,312 KB
testcase_35 AC 47 ms
54,272 KB
testcase_36 AC 47 ms
54,272 KB
testcase_37 AC 47 ms
54,144 KB
testcase_38 AC 46 ms
54,144 KB
testcase_39 AC 49 ms
54,912 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