結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
54,912 KB
testcase_01 AC 49 ms
54,912 KB
testcase_02 AC 48 ms
54,656 KB
testcase_03 AC 48 ms
54,400 KB
testcase_04 AC 49 ms
54,528 KB
testcase_05 AC 49 ms
54,656 KB
testcase_06 AC 50 ms
55,168 KB
testcase_07 AC 49 ms
55,040 KB
testcase_08 AC 49 ms
55,040 KB
testcase_09 AC 49 ms
54,784 KB
testcase_10 AC 277 ms
79,616 KB
testcase_11 AC 267 ms
79,104 KB
testcase_12 AC 248 ms
79,104 KB
testcase_13 AC 262 ms
79,872 KB
testcase_14 AC 302 ms
81,280 KB
testcase_15 AC 270 ms
80,128 KB
testcase_16 AC 303 ms
81,664 KB
testcase_17 AC 318 ms
81,152 KB
testcase_18 AC 310 ms
81,536 KB
testcase_19 AC 292 ms
80,768 KB
testcase_20 AC 182 ms
79,360 KB
testcase_21 AC 182 ms
79,616 KB
testcase_22 AC 125 ms
78,336 KB
testcase_23 AC 173 ms
80,000 KB
testcase_24 AC 200 ms
80,256 KB
testcase_25 AC 118 ms
78,464 KB
testcase_26 AC 152 ms
78,720 KB
testcase_27 AC 105 ms
78,208 KB
testcase_28 AC 127 ms
78,464 KB
testcase_29 AC 103 ms
78,592 KB
testcase_30 AC 81 ms
71,936 KB
testcase_31 AC 82 ms
72,832 KB
testcase_32 AC 82 ms
71,808 KB
testcase_33 AC 81 ms
72,192 KB
testcase_34 AC 82 ms
72,192 KB
testcase_35 AC 49 ms
54,528 KB
testcase_36 AC 49 ms
54,400 KB
testcase_37 AC 49 ms
54,528 KB
testcase_38 AC 49 ms
54,400 KB
testcase_39 AC 50 ms
55,040 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