結果

問題 No.654 Air E869120
ユーザー Mao-betaMao-beta
提出日時 2020-09-24 00:26:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,016 ms / 2,000 ms
コード長 5,099 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 13,440 KB
実行使用メモリ 184,832 KB
最終ジャッジ日時 2024-06-28 04:44:14
合計ジャッジ時間 10,369 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
11,136 KB
testcase_01 AC 33 ms
11,392 KB
testcase_02 AC 32 ms
11,136 KB
testcase_03 AC 34 ms
11,264 KB
testcase_04 AC 33 ms
11,264 KB
testcase_05 AC 32 ms
11,136 KB
testcase_06 AC 34 ms
11,264 KB
testcase_07 AC 33 ms
11,264 KB
testcase_08 AC 33 ms
11,264 KB
testcase_09 AC 33 ms
11,136 KB
testcase_10 AC 261 ms
12,032 KB
testcase_11 AC 182 ms
12,288 KB
testcase_12 AC 195 ms
12,032 KB
testcase_13 AC 239 ms
12,032 KB
testcase_14 AC 182 ms
12,032 KB
testcase_15 AC 187 ms
12,032 KB
testcase_16 AC 233 ms
15,240 KB
testcase_17 AC 262 ms
15,116 KB
testcase_18 AC 234 ms
15,100 KB
testcase_19 AC 254 ms
15,364 KB
testcase_20 AC 129 ms
18,076 KB
testcase_21 AC 148 ms
17,948 KB
testcase_22 AC 74 ms
18,164 KB
testcase_23 AC 93 ms
18,044 KB
testcase_24 AC 145 ms
18,064 KB
testcase_25 AC 69 ms
18,040 KB
testcase_26 AC 84 ms
18,044 KB
testcase_27 AC 75 ms
20,608 KB
testcase_28 AC 107 ms
21,308 KB
testcase_29 AC 74 ms
20,864 KB
testcase_30 AC 990 ms
184,832 KB
testcase_31 AC 1,012 ms
184,832 KB
testcase_32 AC 1,016 ms
184,832 KB
testcase_33 AC 971 ms
153,600 KB
testcase_34 AC 1,008 ms
184,832 KB
testcase_35 AC 32 ms
11,264 KB
testcase_36 AC 32 ms
11,264 KB
testcase_37 AC 32 ms
11,136 KB
testcase_38 AC 32 ms
11,136 KB
testcase_39 AC 32 ms
11,136 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:
    def __init__(self, n=0):
        self._n = n
        self.g = [[] for _ in range(n)]
        self.pos = []

    def add_edge(self, frm, to, cap):
        m = len(self.pos)
        e1 = Dinic._edge(to, cap)
        e2 = Dinic._edge(frm, 0)
        e1.rev = e2
        e2.rev = e1
        self.pos.append(e1)
        self.g[frm].append(e1)
        self.g[to].append(e2)
        return m

    class edge:
        def __init__(self, frm, to, cap, flow):
            self.frm = frm
            self.to = to
            self.cap = cap
            self.flow = flow

        def __iter__(self):
            yield self.frm
            yield self.to
            yield self.cap
            yield self.flow

    def get_edge(self, i):
        e1 = self.pos[i]
        e2 = e1.rev
        return Dinic.edge(e2.to, e1.to, e1.cap + e2.cap, e2.cap)

    def edges(self):
        return [self.get_edge(i) for i in range(len(self.pos))]

    def change_edge(self, i, new_cap, new_flow):
        e = self.pos[i]
        e.cap = new_cap - new_flow
        e.rev.cap = new_flow

    def flow(self, s, t, flow_limit=0XFFFFFFFFFFFFFFF):
        g = self.g
        flow = 0
        while flow < flow_limit:
            level = [-1] * self._n
            level[s] = 0
            que = [None] * self._n
            ql = 0
            qr = 1
            que[0] = s
            unreached = True
            while unreached and ql < qr:
                v = que[ql]
                ql += 1
                for e in g[v]:
                    to = e.to
                    if e.cap and level[to] < 0:
                        level[to] = level[v] + 1
                        if to == t:
                            unreached = False
                            break
                        que[qr] = to
                        qr += 1
            if unreached:
                return flow
            ptr = [len(es) for es in g]
            stack = []
            v = t
            up = flow_limit - flow
            res = 0
            while True:
                if v == s or not ptr[v]:
                    if v == s:
                        res = up
                    while stack:
                        tmp = res
                        e, up, res = stack.pop()
                        e.cap -= tmp
                        e.rev.cap += tmp
                        res += tmp
                        if res < up:
                            v = e.to
                            break
                    else:
                        flow += res
                        break
                i = ptr[v]
                while i:
                    i -= 1
                    e = g[v][i]
                    if level[e.to] == level[v] - 1 and e.rev.cap:
                        ptr[v] = i
                        stack.append((e.rev, up, res))
                        v = e.to
                        up = min(up, e.rev.cap)
                        res = 0
                        break
                else:
                    ptr[v] = i
        return flow

    def min_cut(self, s):
        visited = [False] * self._n
        que = [None] * self._n
        ql = 0
        qr = 1
        que[0] = s
        visited[s] = True
        while ql < qr:
            p = que[ql]
            ql += 1
            for e in self.g[p]:
                if e.cap and not visited[e.to]:
                    visited[e.to] = True
                    que[qr] = e.to
                    qr += 1
        return visited

    class _edge:
        def __init__(self, to, cap):
            self.to = to
            self.cap = cap


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_edge(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_edge(n*T+p, n*T+q, INF)

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

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


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