結果

問題 No.654 Air E869120
ユーザー 👑 timitimi
提出日時 2023-08-10 22:20:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 2,228 bytes
コンパイル時間 133 ms
コンパイル使用メモリ 82,460 KB
実行使用メモリ 80,632 KB
最終ジャッジ日時 2024-04-28 15:20:32
合計ジャッジ時間 5,893 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,872 KB
testcase_01 AC 38 ms
54,856 KB
testcase_02 AC 39 ms
55,848 KB
testcase_03 AC 41 ms
55,408 KB
testcase_04 AC 38 ms
55,736 KB
testcase_05 AC 38 ms
56,048 KB
testcase_06 AC 43 ms
61,860 KB
testcase_07 AC 40 ms
55,372 KB
testcase_08 AC 40 ms
55,308 KB
testcase_09 AC 38 ms
55,248 KB
testcase_10 AC 271 ms
79,132 KB
testcase_11 AC 240 ms
79,436 KB
testcase_12 AC 202 ms
78,680 KB
testcase_13 AC 243 ms
80,096 KB
testcase_14 AC 186 ms
78,612 KB
testcase_15 AC 180 ms
77,424 KB
testcase_16 AC 249 ms
80,632 KB
testcase_17 AC 225 ms
79,516 KB
testcase_18 AC 227 ms
79,664 KB
testcase_19 AC 246 ms
80,572 KB
testcase_20 AC 145 ms
78,076 KB
testcase_21 AC 132 ms
78,324 KB
testcase_22 AC 96 ms
77,884 KB
testcase_23 AC 130 ms
78,672 KB
testcase_24 AC 157 ms
78,724 KB
testcase_25 AC 107 ms
77,616 KB
testcase_26 AC 113 ms
77,876 KB
testcase_27 AC 91 ms
77,640 KB
testcase_28 AC 102 ms
77,536 KB
testcase_29 AC 87 ms
77,688 KB
testcase_30 AC 70 ms
74,480 KB
testcase_31 AC 78 ms
77,452 KB
testcase_32 AC 77 ms
77,540 KB
testcase_33 AC 70 ms
74,388 KB
testcase_34 AC 84 ms
77,684 KB
testcase_35 AC 38 ms
55,160 KB
testcase_36 AC 39 ms
55,448 KB
testcase_37 AC 39 ms
55,284 KB
testcase_38 AC 41 ms
55,188 KB
testcase_39 AC 42 ms
55,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

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, float('inf'))
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, float('inf'))
 
D={}
N,M,d=map(int, input().split())
D[1]=[0]
A=[]
for i in range(M):
  u,v,p,q,w=map(int, input().split())
  A.append((u,v,p,q,w))
  if u not in D:
    D[u]=[]
  D[u].append(p)  
  if v not in D:
    D[v]=[]
  D[v].append(q+d)

F={}
now=0;c=0
for i in range(1,N+1):
  if i in D:
    E=D[i]
    E=sorted(list(set(E)))
    D[i]=E;c+=len(E)
    for e in E:
      F[(i,e)]=now 
      now+=1 

mf=Dinic(c)
for u,v,p,q,w in A:
  x,y=F[(u,p)],F[(v,q+d)]
  mf.add_link(x,y,w)

for e in D:
  G=D[e]
  for i in range(len(G)-1):
    x,y=G[i],G[i+1]
    mf.add_link(F[(e,x)],F[(e,y)],10**12)

print(mf.max_flow(0,c-1))
0