結果

問題 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  
実行時間 1,030 ms / 2,000 ms
コード長 3,135 bytes
コンパイル時間 227 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 169,344 KB
最終ジャッジ日時 2024-06-28 04:44:04
合計ジャッジ時間 9,792 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
11,008 KB
testcase_01 AC 35 ms
11,008 KB
testcase_02 AC 32 ms
11,136 KB
testcase_03 AC 33 ms
11,264 KB
testcase_04 AC 33 ms
11,008 KB
testcase_05 AC 33 ms
11,008 KB
testcase_06 AC 33 ms
11,136 KB
testcase_07 AC 34 ms
11,008 KB
testcase_08 AC 33 ms
11,008 KB
testcase_09 AC 34 ms
11,008 KB
testcase_10 AC 199 ms
11,776 KB
testcase_11 AC 183 ms
11,904 KB
testcase_12 AC 168 ms
12,032 KB
testcase_13 AC 185 ms
11,776 KB
testcase_14 AC 175 ms
11,776 KB
testcase_15 AC 166 ms
11,904 KB
testcase_16 AC 267 ms
14,876 KB
testcase_17 AC 300 ms
14,752 KB
testcase_18 AC 264 ms
14,880 KB
testcase_19 AC 296 ms
14,880 KB
testcase_20 AC 102 ms
17,152 KB
testcase_21 AC 113 ms
17,152 KB
testcase_22 AC 63 ms
17,152 KB
testcase_23 AC 81 ms
17,280 KB
testcase_24 AC 108 ms
17,152 KB
testcase_25 AC 59 ms
17,152 KB
testcase_26 AC 72 ms
17,408 KB
testcase_27 AC 68 ms
19,968 KB
testcase_28 AC 78 ms
19,968 KB
testcase_29 AC 66 ms
19,968 KB
testcase_30 AC 982 ms
169,344 KB
testcase_31 AC 1,005 ms
169,216 KB
testcase_32 AC 1,004 ms
169,344 KB
testcase_33 AC 992 ms
153,472 KB
testcase_34 AC 1,030 ms
169,088 KB
testcase_35 AC 33 ms
11,008 KB
testcase_36 AC 34 ms
11,008 KB
testcase_37 AC 32 ms
11,008 KB
testcase_38 AC 32 ms
11,008 KB
testcase_39 AC 33 ms
11,008 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