結果
問題 | No.654 Air E869120 |
ユーザー | もちふわゆるゆる |
提出日時 | 2023-03-30 08:45:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 251 ms / 2,000 ms |
コード長 | 4,053 bytes |
コンパイル時間 | 240 ms |
コンパイル使用メモリ | 81,900 KB |
実行使用メモリ | 148,480 KB |
最終ジャッジ日時 | 2024-09-21 20:29:20 |
合計ジャッジ時間 | 7,103 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 35 |
ソースコード
import sys, math input = lambda: sys.stdin.readline()[:-1] def MI(): return map(int, input().split()) inf = (1<<63)-1 sys.setrecursionlimit(10**7) # Codeforcesでは350000程度に from collections import * class mf_graph: n=1 g=[[] for i in range(1)] pos=[] def __init__(self,N): self.n=N self.g=[[] for i in range(N)] self.pos=[] def add_edge(self,From,To,cap): assert 0<=From and From<self.n assert 0<=To and To<self.n assert 0<=cap # print(From, To, cap) m=len(self.pos) from_id=len(self.g[From]) self.pos.append([From,from_id]) to_id=len(self.g[To]) if From==To:to_id+=1 self.g[From].append([To,to_id,cap]) self.g[To].append([From,from_id,0]) return m def get_edge(self,i): m=len(self.pos) assert 0<=i and i<m _e=self.g[self.pos[i][0]][self.pos[i][1]] _re=self.g[_e[0]][_e[1]] return [self.pos[i][0],_e[0],_e[2]+_re[2],_re[2]] def edges(self): m=len(self.pos) result=[] for i in range(m): a,b,c,d=self.get_edge(i) result.append({"from":a,"to":b,"cap":c,"flow":d}) return result def change_edge(self,i,new_cap,new_flow): m=len(self.pos) assert 0<=i and i<m assert 0<=new_flow and new_flow<=new_cap _e=self.g[self.pos[i][0]][self.pos[i][1]] _re=self.g[_e[0]][_e[1]] _e[2]=new_cap-new_flow _re[2]=new_flow def flow(self,s,t,flow_limit=(1<<63)-1): assert 0<=s and s<self.n assert 0<=t and t<self.n assert s!=t def bfs(): level=[-1 for i in range(self.n)] level[s]=0 que=deque([]) que.append(s) while(que): v=que.popleft() for to,rev,cap in self.g[v]: if cap==0 or level[to]>=0:continue level[to]=level[v]+1 if to==t:return level que.append(to) return level def dfs(v,up): if (v==s):return up res=0 level_v=level[v] for i in range(Iter[v],len(self.g[v])): to,rev,cap=self.g[v][i] if (level_v<=level[to] or self.g[to][rev][2]==0):continue d=dfs(to,min(up-res,self.g[to][rev][2])) if d<=0:continue self.g[v][i][2]+=d self.g[to][rev][2]-=d res+=d if res==up:return res level[v]=self.n return res flow=0 while(flow<flow_limit): level=bfs() if level[t]==-1:break Iter=[0 for i in range(self.n)] f=dfs(t,flow_limit-flow) if not(f):break flow+=f return flow def min_cut(self,s): visited=[False for i in range(self.n)] que=deque([]) que.append(s) while(len(que)>0): p=que.popleft() visited[p]=True for to,rev,cap in self.g[p]: if cap and not(visited[to]): visited[to]=True que.append(to) return visited n,m,d = MI() events = [] time = set([0, inf]) for i in range(m): u,v,p,q,w = MI() u-=1; v-=1 q += d events.append((u,v,p,q,w)) time.add(p) time.add(q) time = sorted(list(time)) time_dict = { t:i for i,t in enumerate(time) } max_time = len(time) dinic = mf_graph(max_time*n) def conv(v, t): return max_time*v + time_dict[t] wait = [[] for _ in range(n)] for u,v,p,q,w in events: dinic.add_edge(conv(u, p), conv(v, q), w) wait[u].append(p) wait[v].append(q) for i in range(n): wait[i].sort() for u in range(len(wait[i])-1): t1 = wait[i][u] t2 = wait[i][u+1] dinic.add_edge(conv(i, t1), conv(i, t2), inf) if wait[0] and wait[-1]: print(dinic.flow(conv(0, wait[0][0]), conv(n-1, wait[-1][-1]))) else: print(0)