結果

問題 No.654 Air E869120
ユーザー Mao-betaMao-beta
提出日時 2020-09-24 00:23:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 885 ms / 2,000 ms
コード長 3,135 bytes
コンパイル時間 88 ms
コンパイル使用メモリ 11,316 KB
実行使用メモリ 167,152 KB
最終ジャッジ日時 2023-09-10 13:14:30
合計ジャッジ時間 9,105 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,960 KB
testcase_01 AC 20 ms
9,092 KB
testcase_02 AC 21 ms
9,072 KB
testcase_03 AC 21 ms
9,124 KB
testcase_04 AC 21 ms
9,148 KB
testcase_05 AC 20 ms
9,076 KB
testcase_06 AC 21 ms
9,016 KB
testcase_07 AC 20 ms
8,948 KB
testcase_08 AC 20 ms
8,968 KB
testcase_09 AC 21 ms
9,008 KB
testcase_10 AC 167 ms
9,812 KB
testcase_11 AC 149 ms
9,968 KB
testcase_12 AC 139 ms
9,956 KB
testcase_13 AC 156 ms
9,780 KB
testcase_14 AC 146 ms
9,948 KB
testcase_15 AC 141 ms
9,788 KB
testcase_16 AC 226 ms
12,688 KB
testcase_17 AC 259 ms
12,696 KB
testcase_18 AC 219 ms
12,728 KB
testcase_19 AC 239 ms
12,792 KB
testcase_20 AC 85 ms
15,108 KB
testcase_21 AC 95 ms
15,112 KB
testcase_22 AC 47 ms
15,172 KB
testcase_23 AC 64 ms
15,152 KB
testcase_24 AC 88 ms
15,228 KB
testcase_25 AC 44 ms
15,084 KB
testcase_26 AC 56 ms
15,236 KB
testcase_27 AC 51 ms
17,704 KB
testcase_28 AC 61 ms
17,812 KB
testcase_29 AC 49 ms
17,708 KB
testcase_30 AC 850 ms
167,152 KB
testcase_31 AC 871 ms
166,944 KB
testcase_32 AC 885 ms
166,984 KB
testcase_33 AC 873 ms
151,320 KB
testcase_34 AC 850 ms
167,140 KB
testcase_35 AC 20 ms
8,988 KB
testcase_36 AC 20 ms
8,972 KB
testcase_37 AC 20 ms
9,044 KB
testcase_38 AC 20 ms
8,924 KB
testcase_39 AC 21 ms
9,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
INF = 10 ** 20
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


class Dinic:
    """ 最大流(Dinic) """

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

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

    def bfs(self, s):
        from collections import deque

        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)
        self.depth = depth

    def dfs(self, v, t, flow):
        if v == t:
            return flow
        links_v = self.links[v]
        for i in range(self.progress[v], len(links_v)):
            self.progress[v] = i
            cap, to, rev = link = links_v[i]
            if cap == 0 or self.depth[v] >= self.depth[to]:
                continue
            d = self.dfs(to, t, min(flow, cap))
            if d == 0:
                continue
            link[0] -= d
            self.links[to][rev][0] += d
            return d
        return 0

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


def compress(S):
    """ 座標圧縮 """

    zipped, unzipped = {}, {}
    for i, a in enumerate(sorted(S)):
        zipped[a] = i
        unzipped[i] = a
    return zipped, unzipped


def main():
    N, M, d = NMI()
    time_set = set()
    edges = []
    for i in range(M):
        u, v, p, q, w = NMI()
        u, v = u-1, v-1
        q = q + d
        time_set.add(p)
        time_set.add(q)
        edges.append([u, v, p, q, w])
    time_zip, time_unzip = compress(time_set)
    T = len(time_zip)
    din = Dinic(N*T)
    flight_times = [[] for _ in range(N)]
    for u, v, p, q, w in edges:
        p = time_zip[p]
        q = time_zip[q]
        din.add_link(u*T+p, v*T+q, w)
        flight_times[u].append(p)
        flight_times[v].append(q)
    for n in range(N):
        flight_times[n].sort()
        f_len = len(flight_times[n])
        for f in range(f_len-1):
            p, q = flight_times[n][f], flight_times[n][f+1]
            din.add_link(n*T+p, n*T+q, INF)

    if not flight_times[0] or not flight_times[-1]:
        print(0)
        exit()

    print(din.max_flow(0*T+flight_times[0][0], (N-1)*T+flight_times[N-1][-1]))


if __name__ == "__main__":
    main()
0