結果

問題 No.1301 Strange Graph Shortest Path
ユーザー roarisroaris
提出日時 2020-11-28 02:48:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 997 ms / 3,000 ms
コード長 2,062 bytes
コンパイル時間 367 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 188,172 KB
最終ジャッジ日時 2023-10-09 23:23:39
合計ジャッジ時間 30,761 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
70,924 KB
testcase_01 AC 67 ms
71,120 KB
testcase_02 AC 791 ms
177,324 KB
testcase_03 AC 755 ms
166,768 KB
testcase_04 AC 911 ms
181,764 KB
testcase_05 AC 772 ms
180,220 KB
testcase_06 AC 886 ms
173,996 KB
testcase_07 AC 873 ms
175,416 KB
testcase_08 AC 805 ms
167,944 KB
testcase_09 AC 858 ms
168,876 KB
testcase_10 AC 812 ms
167,612 KB
testcase_11 AC 987 ms
177,588 KB
testcase_12 AC 946 ms
177,912 KB
testcase_13 AC 854 ms
179,672 KB
testcase_14 AC 805 ms
168,592 KB
testcase_15 AC 842 ms
170,740 KB
testcase_16 AC 939 ms
182,312 KB
testcase_17 AC 921 ms
182,148 KB
testcase_18 AC 893 ms
172,056 KB
testcase_19 AC 889 ms
175,088 KB
testcase_20 AC 859 ms
173,168 KB
testcase_21 AC 892 ms
179,520 KB
testcase_22 AC 911 ms
175,592 KB
testcase_23 AC 877 ms
179,820 KB
testcase_24 AC 899 ms
173,992 KB
testcase_25 AC 955 ms
182,256 KB
testcase_26 AC 853 ms
175,592 KB
testcase_27 AC 902 ms
176,596 KB
testcase_28 AC 826 ms
176,292 KB
testcase_29 AC 912 ms
181,216 KB
testcase_30 AC 969 ms
181,084 KB
testcase_31 AC 945 ms
181,056 KB
testcase_32 AC 70 ms
70,792 KB
testcase_33 AC 441 ms
172,484 KB
testcase_34 AC 997 ms
188,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from heapq import *

class Mincostflow:
    def __init__(self, N):
        self.N = N
        self.G = [[] for _ in range(N)]
        
    def add_edge(self, u, v, cap, cost):
        self.G[u].append([v, cap, cost, len(self.G[v])])
        self.G[v].append([u, 0, -cost, len(self.G[u])-1])
    
    def min_cost_flow(self, s, t, f):
        res = 0
        h = [0]*self.N
        
        while f>0:
            dist = [10**18]*self.N
            dist[s] = 0
            pq = [(0, s)]
            prev_v = [-1]*self.N
            prev_e = [-1]*self.N
            
            while pq:
                d, v = heappop(pq)
                
                if dist[v]<d:
                    continue
                
                for i in range(len(self.G[v])):
                    nv, cap, cost, _  = self.G[v][i]
                    
                    if cap>0 and dist[nv]>dist[v]+cost+h[v]-h[nv]:
                        dist[nv] = dist[v]+cost+h[v]-h[nv]
                        prev_v[nv] = v
                        prev_e[nv] = i
                        heappush(pq, (dist[nv], nv))
            
            if dist[t]==10**18:
                return -1
            
            for i in range(N):
                h[i] += dist[i]
                
            d = f
            v = t
            
            while v!=s:
                d = min(d, self.G[prev_v[v]][prev_e[v]][1])
                v = prev_v[v]
            
            f -= d
            res += d*h[t]
            v = t
            
            while v!=s:
                self.G[prev_v[v]][prev_e[v]][1] -= d
                rev = self.G[prev_v[v]][prev_e[v]][3]
                self.G[v][rev][1] += d
                v = prev_v[v]
        
        return res

N, M = map(int, input().split())
mcf = Mincostflow(N)

for _ in range(M):
    u, v, c, d = map(int, input().split())
    mcf.add_edge(u-1, v-1, 1, c)
    mcf.add_edge(v-1, u-1, 1, c)
    mcf.add_edge(u-1, v-1, 1, d)
    mcf.add_edge(v-1, u-1, 1, d)

print(mcf.min_cost_flow(0, N-1, 2))
0