結果

問題 No.654 Air E869120
ユーザー EguyEguy
提出日時 2021-10-24 16:45:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 2,556 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 81,576 KB
最終ジャッジ日時 2024-04-10 01:38:43
合計ジャッジ時間 6,473 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
56,472 KB
testcase_01 AC 46 ms
56,144 KB
testcase_02 AC 41 ms
55,076 KB
testcase_03 AC 43 ms
54,576 KB
testcase_04 AC 44 ms
54,760 KB
testcase_05 AC 44 ms
55,848 KB
testcase_06 AC 46 ms
56,588 KB
testcase_07 AC 43 ms
56,888 KB
testcase_08 AC 44 ms
55,828 KB
testcase_09 AC 45 ms
55,428 KB
testcase_10 AC 261 ms
79,816 KB
testcase_11 AC 239 ms
79,136 KB
testcase_12 AC 227 ms
79,060 KB
testcase_13 AC 241 ms
79,560 KB
testcase_14 AC 282 ms
81,120 KB
testcase_15 AC 246 ms
79,740 KB
testcase_16 AC 274 ms
81,568 KB
testcase_17 AC 297 ms
81,096 KB
testcase_18 AC 289 ms
81,576 KB
testcase_19 AC 268 ms
81,156 KB
testcase_20 AC 161 ms
79,816 KB
testcase_21 AC 161 ms
79,668 KB
testcase_22 AC 107 ms
78,224 KB
testcase_23 AC 153 ms
80,188 KB
testcase_24 AC 182 ms
80,308 KB
testcase_25 AC 104 ms
78,844 KB
testcase_26 AC 139 ms
79,156 KB
testcase_27 AC 87 ms
78,276 KB
testcase_28 AC 113 ms
78,316 KB
testcase_29 AC 89 ms
78,508 KB
testcase_30 AC 68 ms
73,004 KB
testcase_31 AC 70 ms
73,816 KB
testcase_32 AC 69 ms
73,028 KB
testcase_33 AC 71 ms
74,028 KB
testcase_34 AC 73 ms
72,464 KB
testcase_35 AC 43 ms
55,712 KB
testcase_36 AC 44 ms
55,332 KB
testcase_37 AC 43 ms
54,432 KB
testcase_38 AC 43 ms
55,452 KB
testcase_39 AC 45 ms
56,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, math
sys.setrecursionlimit(10**6)
INF = float('inf')
mod = 10**9 + 7
#mod = 998244353
input = lambda: sys.stdin.readline().rstrip()
def li(): return list(map(int, input().split()))
from collections import deque, defaultdict

class Dinic:
    def __init__(self, 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):
        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
        links_v = self.links[v]
        for i in range(self.progress[v], len(links_v)):
            self.progress[v] = i
            cap, to, rev = link = 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
            link[0] -= d
            self.links[to][rev][0] += d
            return d
        return 0
 
    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.depth[t] < 0:
                return flow
            self.progress = [0] * self.n
            current_flow = self.dfs(s, t, INF)
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, INF)


N, M, d = li()
lists = [[] for _ in range(N+1)]
inp = []
for _ in range(M):
    u, v, p, q, w = li()
    q += d
    lists[u].append(p)
    lists[v].append(q)
    inp.append((u, v, p, q, w))

dicts = []
acc = [0]
for i in range(N+1):
    lists[i] = list(set(lists[i]))
    lists[i].sort()
    d = {i: e for e, i in enumerate(lists[i])}
    dicts.append(d)
    acc.append(acc[-1] + len(lists[i]))

dinic = Dinic(acc[-1] + 2)
for i in range(M):
    u, v, p, q, w = inp[i]
    idx1 = acc[u] + dicts[u][p]
    idx2 = acc[v] + dicts[v][q]
    dinic.add_link(idx1, idx2, w)

t = acc[-1] + 1
for i in range(1, N+1):
    for j in range(len(dicts[i])-1):
        idx = acc[i] + j
        dinic.add_link(idx, idx+1, INF)

print(dinic.max_flow(0, acc[-1]-1 ))
0