結果

問題 No.654 Air E869120
ユーザー petite_prog_twitterpetite_prog_twitter
提出日時 2019-09-06 18:45:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 4,447 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 11,132 KB
実行使用メモリ 10,672 KB
最終ジャッジ日時 2023-09-06 14:03:57
合計ジャッジ時間 4,711 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
9,324 KB
testcase_01 AC 24 ms
9,228 KB
testcase_02 AC 24 ms
9,356 KB
testcase_03 AC 24 ms
9,264 KB
testcase_04 AC 24 ms
9,364 KB
testcase_05 AC 24 ms
9,348 KB
testcase_06 AC 24 ms
9,388 KB
testcase_07 AC 24 ms
9,364 KB
testcase_08 AC 24 ms
9,192 KB
testcase_09 AC 24 ms
9,320 KB
testcase_10 AC 125 ms
9,792 KB
testcase_11 AC 113 ms
9,780 KB
testcase_12 AC 107 ms
9,768 KB
testcase_13 AC 118 ms
9,708 KB
testcase_14 AC 113 ms
9,684 KB
testcase_15 AC 106 ms
9,704 KB
testcase_16 AC 209 ms
10,476 KB
testcase_17 AC 228 ms
10,592 KB
testcase_18 AC 204 ms
10,604 KB
testcase_19 AC 216 ms
10,560 KB
testcase_20 AC 70 ms
10,536 KB
testcase_21 AC 76 ms
10,520 KB
testcase_22 AC 38 ms
10,544 KB
testcase_23 AC 51 ms
10,404 KB
testcase_24 AC 70 ms
10,360 KB
testcase_25 AC 35 ms
10,484 KB
testcase_26 AC 44 ms
10,540 KB
testcase_27 AC 33 ms
10,492 KB
testcase_28 AC 39 ms
10,384 KB
testcase_29 AC 31 ms
10,340 KB
testcase_30 AC 32 ms
10,564 KB
testcase_31 AC 31 ms
10,596 KB
testcase_32 AC 31 ms
10,652 KB
testcase_33 AC 30 ms
10,640 KB
testcase_34 AC 31 ms
10,672 KB
testcase_35 AC 24 ms
9,316 KB
testcase_36 AC 24 ms
9,248 KB
testcase_37 AC 24 ms
9,312 KB
testcase_38 AC 24 ms
9,184 KB
testcase_39 AC 24 ms
9,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#
#     ⋀_⋀  
#    (・ω・)  
# ./ U ∽ U\
#  │* 合 *│
#  │* 格 *│ 
#  │* 祈 *│ 
#  │* 願 *│ 
#  │*   *│ 
#       ̄
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないyp
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
class Dinitz:
    #edge := (to, capa, rev_index)

    def __init__(self, N):
        self.__N = N
        self.__Graph = [[] for _ in range(N)] #to, capa, rev_index
        self.__level = [0] * N
        self.__iter = [0]*N

    def add_edge(self, from_, to, capa):
        self.__Graph[from_].append([to, capa, len(self.__Graph[to])])
        self.__Graph[to].append([from_, 0, len(self.__Graph[from_])-1 ])
    
    def solve(self, source:int, target:int):
        self.__target = target
        flow = 0
        while self.leveling_bfs(source):
            self.__iter = [0] * self.__N
            while True:
                delta = self.dfs(source, inf)
                if not delta:
                    break
                flow += delta
        return flow


    def leveling_bfs(self, source: int) -> bool:
        self.__level = [-1] * self.__N
        self.__level[source] = 0
        queue = deque([source])

        pop = queue.popleft
        append = queue.append
        while queue:
            from_ = pop()
            for to, capa, _ in self.__Graph[from_]:
                if capa > 0 > self.__level[to]:
                    self.__level[to] = self.__level[from_] + 1
                    append(to)
        return self.__level[self.__target] != -1 #到達可能ならTrue

    def dfs(self, from_:int,  flow:int) ->int:
        if from_ == self.__target:
            return flow
        for i in range(self.__iter[from_], len(self.__Graph[from_])):
            self.__iter[from_] = i
            to, capa, rev = edge = self.__Graph[from_][i]
            if capa and self.__level[from_] < self.__level[to]:
                delta = self.dfs(to, min(flow,capa))
                if delta: #増加パス
                    edge[1] -= delta #capaをへらす
                    self.__Graph[to][rev][1] += delta #逆辺のcapaを増やす
                    return delta
        return 0
    
def main():
    N,M,D = MI()
    UVPQW = []

    times = [set()for _ in range(N)]
    for _ in range(M):
        u,v,p,q,w = MI()
        u-=1
        v-=1
        UVPQW.append((u,v,p,q,w))
        times[u].add(p)
        times[v].add(q+D)
    
    for i,n in enumerate(times):
        times[i] = sorted(list(n))

    ID = [{} for _ in range(N)]
    cnt = 0
    for i,time in enumerate(times):
        for t in time:
            ID[i][t] = cnt
            cnt += 1

    dinic = Dinitz(cnt)
    add_edge = dinic.add_edge
    solve = dinic.solve

    #飛行機で移動する辺
    for u,v,p,q,w in UVPQW:
        from_ = ID[u][p]
        to = ID[v][q+D]
        add_edge(from_, to, w)
    #空港で待つ辺
    for i,time in enumerate(times):
        for j,t in enumerate(time[:-1]):
            from_ = ID[i][t]
            to_time = time[j+1]
            to = ID[i][to_time]
            add_edge(from_, to, inf)
    
    ans = solve(0,cnt-1)
    print(ans)
    



        
    edges = [[]for _ in range(N)]
    



    """
    for _ in range(M):
        edges[u].append(v)
        nodes

    #
    
    uvpqw=[]
    for _ in range(M):
        u-=1
        v-=1
        uvpqw.append()

    ans = solve(0,N-1)"""

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