結果

問題 No.654 Air E869120
ユーザー AEnAEn
提出日時 2022-11-03 16:04:40
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,754 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 87,336 KB
実行使用メモリ 80,676 KB
最終ジャッジ日時 2023-09-25 00:54:08
合計ジャッジ時間 8,376 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
72,104 KB
testcase_01 AC 92 ms
71,648 KB
testcase_02 AC 90 ms
71,816 KB
testcase_03 AC 93 ms
71,892 KB
testcase_04 AC 91 ms
71,724 KB
testcase_05 AC 91 ms
71,808 KB
testcase_06 AC 93 ms
72,464 KB
testcase_07 AC 94 ms
72,148 KB
testcase_08 AC 93 ms
72,508 KB
testcase_09 AC 90 ms
71,720 KB
testcase_10 AC 197 ms
78,784 KB
testcase_11 AC 194 ms
78,812 KB
testcase_12 AC 198 ms
79,196 KB
testcase_13 AC 200 ms
79,104 KB
testcase_14 AC 193 ms
78,856 KB
testcase_15 AC 197 ms
78,808 KB
testcase_16 WA -
testcase_17 AC 236 ms
80,200 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 185 ms
80,132 KB
testcase_21 AC 191 ms
80,284 KB
testcase_22 AC 167 ms
79,984 KB
testcase_23 AC 186 ms
80,160 KB
testcase_24 WA -
testcase_25 AC 149 ms
79,844 KB
testcase_26 AC 162 ms
80,252 KB
testcase_27 AC 150 ms
79,708 KB
testcase_28 WA -
testcase_29 AC 146 ms
79,580 KB
testcase_30 AC 132 ms
78,004 KB
testcase_31 AC 128 ms
77,712 KB
testcase_32 AC 125 ms
77,848 KB
testcase_33 AC 127 ms
78,020 KB
testcase_34 AC 131 ms
78,068 KB
testcase_35 AC 90 ms
71,972 KB
testcase_36 AC 88 ms
71,916 KB
testcase_37 AC 89 ms
71,896 KB
testcase_38 AC 88 ms
71,804 KB
testcase_39 AC 92 ms
72,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
sys.setrecursionlimit(10**8)
import pypyjit
pypyjit.set_param("max_unroll_recursion=-1")
 
class Dinic:
    def __init__(self, n):
        """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):
        """sからtへの残余ネットワーク上での最短距離"""
        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
        for i in range(self.progress[v], len(self.links[v])):
            self.progress[v] = i
            cap, to, rev = self.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
            # 残余ネットワークの更新
            self.links[v][i][0] -= d
            self.links[to][rev][0] += d
            return d
        return 0
 
    def max_flow(self, s, t):
        """最大フローを求める"""
        flow = 0
        while True:
            # tに到達できるか
            self.bfs(s)
            if self.depth[t] < 0:
                return flow
            self.progress = [0] * self.n
            current_flow = self.dfs(s, t, float('inf'))
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, float('inf'))

N, M, d = map(int, input().split())
G = [set() for _ in range(N)]
G[0].add(0)
G[-1].add(10**9)
edge = []
for i in range(M):
    u, v, p, q, w = map(int, input().split())
    u-=1;v-=1
    G[u].add(p)
    G[v].add(q+d)
    edge.append([u, v, p, q, w])
ver = {}
ver[(0,0)] = 0
for i in range(N):
    G[i] = list(G[i])
    G[i].sort()

for i in range(N):
    for j in range(len(G[i])):
        ver[(i, G[i][j])] = len(ver)

dinic = Dinic(len(ver))
for i in range(N):
    for j in range(len(G[i])-1):
        dinic.add_link(ver[(i, G[i][j])], ver[(i, G[i][j+1])], float('inf'))

for u, v, p, q, w in edge:
    dinic.add_link(ver[(u, p)], ver[(v, q+d)], w)

print(dinic.max_flow(ver[(0,0)],ver[(N-1,10**9)]))
0