結果

問題 No.1301 Strange Graph Shortest Path
ユーザー とりゐとりゐ
提出日時 2022-11-10 00:28:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,567 ms / 3,000 ms
コード長 4,199 bytes
コンパイル時間 421 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 420,956 KB
最終ジャッジ日時 2024-07-23 05:12:45
合計ジャッジ時間 72,793 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,120 KB
testcase_01 AC 40 ms
53,760 KB
testcase_02 AC 2,246 ms
407,932 KB
testcase_03 AC 2,004 ms
368,740 KB
testcase_04 AC 2,457 ms
418,536 KB
testcase_05 AC 2,420 ms
411,432 KB
testcase_06 AC 2,344 ms
388,896 KB
testcase_07 AC 2,255 ms
393,708 KB
testcase_08 AC 2,155 ms
371,936 KB
testcase_09 AC 1,883 ms
373,680 KB
testcase_10 AC 2,035 ms
371,420 KB
testcase_11 AC 2,262 ms
401,556 KB
testcase_12 AC 2,128 ms
400,696 KB
testcase_13 AC 2,091 ms
409,392 KB
testcase_14 AC 2,375 ms
373,200 KB
testcase_15 AC 1,942 ms
378,216 KB
testcase_16 AC 2,377 ms
418,128 KB
testcase_17 AC 2,336 ms
415,796 KB
testcase_18 AC 2,399 ms
384,368 KB
testcase_19 AC 1,958 ms
391,452 KB
testcase_20 AC 2,188 ms
385,416 KB
testcase_21 AC 2,272 ms
408,156 KB
testcase_22 AC 2,478 ms
393,224 KB
testcase_23 AC 2,190 ms
411,444 KB
testcase_24 AC 2,360 ms
389,408 KB
testcase_25 AC 2,375 ms
415,564 KB
testcase_26 AC 2,277 ms
394,632 KB
testcase_27 AC 2,019 ms
401,052 KB
testcase_28 AC 2,145 ms
395,316 KB
testcase_29 AC 2,567 ms
415,688 KB
testcase_30 AC 2,154 ms
413,772 KB
testcase_31 AC 2,257 ms
412,480 KB
testcase_32 AC 40 ms
53,500 KB
testcase_33 AC 1,287 ms
407,640 KB
testcase_34 AC 2,166 ms
420,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
class mcf_graph():
    n=1
    pos=[]
    g=[[]]
    def __init__(self,N):
        self.n=N
        self.pos=[]
        self.g=[[] for i in range(N)]
    def add_edge(self,From,To,cap,cost):
        assert 0<=From and From<self.n
        assert 0<=To and To<self.n
        m=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 result
    def 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.n
        assert 0<=t and t<self.n
        assert s!=t
        '''
         variants (C = maxcost):
         -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0
         reduced 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]=-1
                pe[i]=-1
                vis[i]=False
            que=[]
            heapq.heappush(que,(0,s))
            dist[s]=0
            while(que):
                v=heapq.heappop(que)[1]
                if vis[v]:continue
                vis[v]=True
                if 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)C
                     cost <= 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]+cost
                        pv[e["to"]]=v
                        pe[e["to"]]=i
                        heapq.heappush(que,(dist[e["to"]],e["to"]))
            if not(vis[t]):
                return False
            for v in range(self.n):
                if not(vis[v]):continue
                dual[v]-=dist[t]-dist[v]
            return True
        flow=0
        cost=0
        prev_cost=-1
        result=[(flow,cost)]
        while(flow<flow_limit):
            if not(dual_ref()):
                break
            c=flow_limit-flow
            v=t
            while(v!=s):
                c=min(c,self.g[pv[v]][pe[v]]["cap"])
                v=pv[v]
            v=t
            while(v!=s):
                self.g[pv[v]][pe[v]]["cap"]-=c
                self.g[v][self.g[pv[v]][pe[v]]["rev"]]["cap"]+=c
                v=pv[v]
            d=-dual[s]
            flow+=c
            cost+=c*d
            if(prev_cost==d):
                result.pop()
            result.append((flow,cost))
            prev_cost=cost
        return result


from sys import stdin
input=lambda :stdin.readline()[:-1]


n,m=map(int,input().split())
g=mcf_graph(n)
for _ in range(m):
  u,v,c,d=map(int,input().split())
  u-=1
  v-=1
  g.add_edge(u,v,1,c)
  g.add_edge(u,v,1,d)
  g.add_edge(v,u,1,c)
  g.add_edge(v,u,1,d)

print(g.flow(0,n-1,2)[1])
0