結果

問題 No.654 Air E869120
ユーザー kamojirokamojiro
提出日時 2020-06-10 23:34:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,209 bytes
コンパイル時間 476 ms
コンパイル使用メモリ 11,060 KB
実行使用メモリ 14,788 KB
最終ジャッジ日時 2023-09-06 00:32:02
合計ジャッジ時間 10,414 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
13,240 KB
testcase_01 AC 19 ms
8,956 KB
testcase_02 AC 19 ms
8,736 KB
testcase_03 AC 20 ms
8,736 KB
testcase_04 AC 20 ms
8,932 KB
testcase_05 AC 20 ms
8,824 KB
testcase_06 AC 20 ms
8,784 KB
testcase_07 AC 20 ms
8,788 KB
testcase_08 AC 19 ms
8,772 KB
testcase_09 AC 20 ms
8,728 KB
testcase_10 AC 1,387 ms
9,292 KB
testcase_11 AC 719 ms
9,256 KB
testcase_12 AC 836 ms
9,256 KB
testcase_13 AC 1,205 ms
9,300 KB
testcase_14 AC 584 ms
9,200 KB
testcase_15 AC 645 ms
9,376 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class FordFulkerson:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def add_multi_edge(self, v1, v2, cap1, cap2):
        edge1 = [v2, cap1, None]
        edge1[2] = edge2 = [v1, cap2, edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)

    def dfs(self, v, t, f):
        if v == t:
            return f
        used = self.used
        used[v] = 1
        for e in self.G[v]:
            w, cap, rev = e
            if cap and not used[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        f = INF
        N = self.N 
        while f:
            self.used = [0]*N
            f = self.dfs(s, t, INF)
            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
    ff = FordFulkerson(I)
    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)]
        ff.add_edge(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])]
            ff.add_edge(a,b,INF)
    
    s = VtoI[(0,0)]
    g = VtoI[(N-1,L)]
    print( ff.flow(s,g))
    
if __name__ == '__main__':
    main()
0