結果

問題 No.654 Air E869120
ユーザー AEnAEn
提出日時 2022-11-03 16:20:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 2,777 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 87,380 KB
実行使用メモリ 80,608 KB
最終ジャッジ日時 2023-09-25 01:04:03
合計ジャッジ時間 8,013 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
72,516 KB
testcase_01 AC 93 ms
71,648 KB
testcase_02 AC 90 ms
72,048 KB
testcase_03 AC 90 ms
71,928 KB
testcase_04 AC 90 ms
71,856 KB
testcase_05 AC 89 ms
71,836 KB
testcase_06 AC 93 ms
72,592 KB
testcase_07 AC 93 ms
72,436 KB
testcase_08 AC 95 ms
72,188 KB
testcase_09 AC 93 ms
72,040 KB
testcase_10 AC 212 ms
79,496 KB
testcase_11 AC 212 ms
79,100 KB
testcase_12 AC 202 ms
78,916 KB
testcase_13 AC 208 ms
79,464 KB
testcase_14 AC 201 ms
79,032 KB
testcase_15 AC 198 ms
79,060 KB
testcase_16 AC 223 ms
80,532 KB
testcase_17 AC 243 ms
80,244 KB
testcase_18 AC 228 ms
80,608 KB
testcase_19 AC 237 ms
80,496 KB
testcase_20 AC 182 ms
80,148 KB
testcase_21 AC 188 ms
80,240 KB
testcase_22 AC 170 ms
80,036 KB
testcase_23 AC 169 ms
80,036 KB
testcase_24 AC 187 ms
80,032 KB
testcase_25 AC 152 ms
80,092 KB
testcase_26 AC 178 ms
80,108 KB
testcase_27 AC 161 ms
79,720 KB
testcase_28 AC 162 ms
79,756 KB
testcase_29 AC 146 ms
80,008 KB
testcase_30 AC 128 ms
78,044 KB
testcase_31 AC 134 ms
78,144 KB
testcase_32 AC 131 ms
77,888 KB
testcase_33 AC 131 ms
78,108 KB
testcase_34 AC 129 ms
78,080 KB
testcase_35 AC 92 ms
71,692 KB
testcase_36 AC 91 ms
71,692 KB
testcase_37 AC 90 ms
71,740 KB
testcase_38 AC 89 ms
71,740 KB
testcase_39 AC 93 ms
72,204 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)]
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 = {}
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)

if len(G[0])>0 and len(G[-1])>0:
    print(dinic.max_flow(ver[(0,G[0][0])],ver[(N-1,G[N-1][-1])]))
else:
    print(0)
0