結果

問題 No.654 Air E869120
ユーザー kamojirokamojiro
提出日時 2020-06-11 19:47:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 3,315 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-06-24 03:15:33
合計ジャッジ時間 4,479 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
11,136 KB
testcase_01 AC 35 ms
11,136 KB
testcase_02 AC 34 ms
11,136 KB
testcase_03 AC 34 ms
11,136 KB
testcase_04 AC 34 ms
11,264 KB
testcase_05 AC 34 ms
11,136 KB
testcase_06 AC 34 ms
11,136 KB
testcase_07 AC 34 ms
11,136 KB
testcase_08 AC 35 ms
11,264 KB
testcase_09 AC 34 ms
11,264 KB
testcase_10 AC 126 ms
11,648 KB
testcase_11 AC 115 ms
11,520 KB
testcase_12 AC 108 ms
11,520 KB
testcase_13 AC 116 ms
11,520 KB
testcase_14 AC 111 ms
11,520 KB
testcase_15 AC 107 ms
11,648 KB
testcase_16 WA -
testcase_17 AC 212 ms
12,672 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 74 ms
12,416 KB
testcase_21 AC 77 ms
12,416 KB
testcase_22 AC 47 ms
12,416 KB
testcase_23 AC 60 ms
12,416 KB
testcase_24 WA -
testcase_25 AC 43 ms
12,160 KB
testcase_26 AC 51 ms
12,416 KB
testcase_27 AC 44 ms
12,416 KB
testcase_28 WA -
testcase_29 AC 43 ms
12,160 KB
testcase_30 AC 44 ms
12,544 KB
testcase_31 AC 42 ms
12,672 KB
testcase_32 AC 44 ms
12,672 KB
testcase_33 AC 44 ms
12,544 KB
testcase_34 AC 43 ms
12,544 KB
testcase_35 AC 34 ms
11,136 KB
testcase_36 AC 33 ms
11,264 KB
testcase_37 AC 34 ms
11,136 KB
testcase_38 AC 34 ms
11,136 KB
testcase_39 AC 35 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import defaultdict
from collections import deque
INF = 10**18

class Dinic:
    def __init__(self, N, source, sink):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.source = source
        self.sink = sink

    def add_edge(self, fr, to, cap):
        n1 = len(self.G[fr])
        n2 = len(self.G[to])
        self.G[fr].append([to, cap, n2])
        self.G[to].append([fr, 0, n1])  # 逆辺を cap 0 で追加

    def add_edge_undirected(self, fr, to, cap):
        n1 = len(self.G[fr])
        n2 = len(self.G[to])
        self.G[fr].append([to, cap, n2])
        self.G[to].append([fr, cap, n1])

    def bfs(self):
        level = [0] * self.N
        G = self.G; source = self.source; sink = self.sink
        q = deque([source])
        level[source] = 1
        pop = q.popleft; append = q.append
        while q:
            v = pop()
            lv = level[v] + 1
            for to, cap, rev in G[v]:
                if not cap:
                    continue
                if level[to]:
                    continue
                level[to] = lv
                if to == sink:
                    self.level = level
                    return
                append(to)
        self.level = level

    def dfs(self, v, f):
        if v == self.sink:
            return f
        G = self.G
        prog = self.progress
        level = self.level
        lv = level[v]
        E = G[v]
        for i in range(prog[v], len(E)):
            to, cap, rev = E[i]
            prog[v] = i
            if not cap:
                continue
            if level[to] <= lv:
                continue
            x = f if f < cap else cap
            ff = self.dfs(to, x)
            if ff:
                E[i][1] -= ff
                G[to][rev][1] += ff
                return ff
        return 0

    def max_flow(self):
        INF = 10**18
        flow = 0
        while True:
            self.bfs()
            if not self.level[self.sink]:
                return flow
            self.progress = [0] * self.N
            while True:
                f = self.dfs(self.source, INF)
                if not f:
                    break
                flow += f
        return flow
def main():
    N, M, d = map( int, input().split())
    UVPQW = [ tuple( map( int, input().split())) for _ in range(M)]
    E = [set() for _ in range(N)]
    L = 10**9
    E[0].add(0)
    E[N-1].add(L)
    for u, v, p, q, _ in UVPQW:
        u, v = u-1, v-1
        if q+d <= L:
            E[u].add(p)
            E[v].add(q+d)
    F = [sorted(list(E[i])) for i in range(N)]
    index = 0
    VtoI = defaultdict( int)
    for i in range(N):
        for v in F[i]:
            VtoI[(i,v)] = index
            index += 1
    I = index
    s = VtoI[(0,0)]
    g = VtoI[(N-1,L)]

    dinic = Dinic(I,s,g)
    add = dinic.add_edge
    for u,v,p,q,w in UVPQW:
        u, v = u-1, v-1
        q += d
        if q > L:
            continue
        a = VtoI[(u,p)]
        b = VtoI[(v,q)]
        add(a,b,w)
    for i in range(N):
        lg = len(F[i])
        for j in range(lg-1):
            a = VtoI[(i,F[i][j])]
            b = VtoI[(i, F[i][j+1])]
            add(a,b,INF)
    
    print( dinic.max_flow())
    
if __name__ == '__main__':
    main()
0