結果
問題 |
No.2604 Initial Motion
|
ユーザー |
![]() |
提出日時 | 2024-01-19 12:09:30 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,426 bytes |
コンパイル時間 | 848 ms |
コンパイル使用メモリ | 82,220 KB |
実行使用メモリ | 142,504 KB |
最終ジャッジ日時 | 2024-09-28 03:23:00 |
合計ジャッジ時間 | 8,601 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 11 TLE * 3 -- * 25 |
ソースコード
import sysinput = sys.stdin.readline#https://github.com/shakayami/ACL-for-python/blob/master/mincostflow.pyimport heapqclass mcf_graph():n=1pos=[]g=[[]]def __init__(self,N):self.n=Nself.pos=[]self.g=[[] for i in range(N)]def add_edge(self,From,To,cap,cost):assert 0<=From and From<self.nassert 0<=To and To<self.nm=len(self.pos)self.pos.append((From,len(self.g[From])))self.g[From].append({"to":To,"rev":len(self.g[To]),"cap":cap,"cost":cost})self.g[To].append({"to":From,"rev":len(self.g[From])-1,"cap":0,"cost":-cost})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["to"]][_e["rev"]]return {"from":self.pos[i][0],"to":_e["to"],"cap":_e["cap"]+_re["cap"],"flow":_re["cap"],"cost":_e["cost"]}def edges(self):m=len(self.pos)result=[{} for i in range(m)]for i in range(m):tmp=self.get_edge(i)result[i]["from"]=tmp["from"]result[i]["to"]=tmp["to"]result[i]["cap"]=tmp["cap"]result[i]["flow"]=tmp["flow"]result[i]["cost"]=tmp["cost"]return resultdef flow(self,s,t,flow_limit=-1-(-1<<63)):return self.slope(s,t,flow_limit)[-1]def slope(self,s,t,flow_limit=-1-(-1<<63)):assert 0<=s and s<self.nassert 0<=t and t<self.nassert s!=t'''variants (C = maxcost):-(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge'''dual=[0 for i in range(self.n)]dist=[0 for i in range(self.n)]pv=[0 for i in range(self.n)]pe=[0 for i in range(self.n)]vis=[False for i in range(self.n)]def dual_ref():for i in range(self.n):dist[i]=-1-(-1<<63)pv[i]=-1pe[i]=-1vis[i]=Falseque=[]heapq.heappush(que,(0,s))dist[s]=0while(que):v=heapq.heappop(que)[1]if vis[v]:continuevis[v]=Trueif v==t:break'''dist[v] = shortest(s, v) + dual[s] - dual[v]dist[v] >= 0 (all reduced cost are positive)dist[v] <= (n-1)C'''for i in range(len(self.g[v])):e=self.g[v][i]if vis[e["to"]] or (not(e["cap"])):continue'''|-dual[e.to]+dual[v]| <= (n-1)Ccost <= C - -(n-1)C + 0 = nC'''cost=e["cost"]-dual[e["to"]]+dual[v]if dist[e["to"]]-dist[v]>cost:dist[e["to"]]=dist[v]+costpv[e["to"]]=vpe[e["to"]]=iheapq.heappush(que,(dist[e["to"]],e["to"]))if not(vis[t]):return Falsefor v in range(self.n):if not(vis[v]):continuedual[v]-=dist[t]-dist[v]return Trueflow=0cost=0prev_cost=-1result=[(flow,cost)]while(flow<flow_limit):if not(dual_ref()):breakc=flow_limit-flowv=twhile(v!=s):c=min(c,self.g[pv[v]][pe[v]]["cap"])v=pv[v]v=twhile(v!=s):self.g[pv[v]][pe[v]]["cap"]-=cself.g[v][self.g[pv[v]][pe[v]]["rev"]]["cap"]+=cv=pv[v]d=-dual[s]flow+=ccost+=c*dif(prev_cost==d):result.pop()result.append((flow,cost))prev_cost=costreturn resultK, N, M = map(int, input().split())A = list(map(int, input().split()))B = list(map(int, input().split()))G = mcf_graph(N + 2)s, g = 0, N + 1for i in range(M):u, v, d = map(int, input().split())G.add_edge(u, v, K, d)G.add_edge(v, u, K, d)for i in range(K):G.add_edge(s, A[i], 1, 0)for i in range(N):G.add_edge(i + 1, g, B[i], 0)print(G.flow(s, g, K)[1])