結果

問題 No.1301 Strange Graph Shortest Path
ユーザー roarisroaris
提出日時 2020-11-28 02:31:48
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,066 bytes
コンパイル時間 619 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 258,032 KB
最終ジャッジ日時 2023-10-09 23:22:15
合計ジャッジ時間 38,312 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
258,032 KB
testcase_01 AC 79 ms
71,340 KB
testcase_02 AC 940 ms
179,012 KB
testcase_03 AC 906 ms
166,728 KB
testcase_04 AC 1,073 ms
183,720 KB
testcase_05 AC 921 ms
179,728 KB
testcase_06 AC 1,036 ms
176,024 KB
testcase_07 AC 992 ms
176,228 KB
testcase_08 AC 879 ms
168,032 KB
testcase_09 AC 990 ms
170,964 KB
testcase_10 AC 918 ms
167,540 KB
testcase_11 AC 1,114 ms
178,616 KB
testcase_12 AC 1,019 ms
178,632 KB
testcase_13 AC 1,046 ms
180,792 KB
testcase_14 AC 964 ms
170,016 KB
testcase_15 AC 939 ms
171,324 KB
testcase_16 AC 1,053 ms
183,796 KB
testcase_17 AC 1,050 ms
182,880 KB
testcase_18 AC 1,043 ms
172,172 KB
testcase_19 AC 1,006 ms
176,056 KB
testcase_20 AC 990 ms
174,896 KB
testcase_21 AC 995 ms
179,692 KB
testcase_22 AC 1,030 ms
177,184 KB
testcase_23 AC 1,019 ms
180,384 KB
testcase_24 AC 999 ms
175,764 KB
testcase_25 AC 1,168 ms
183,156 KB
testcase_26 AC 983 ms
176,336 KB
testcase_27 AC 1,016 ms
177,740 KB
testcase_28 AC 922 ms
176,492 KB
testcase_29 AC 1,051 ms
183,608 KB
testcase_30 AC 1,225 ms
183,548 KB
testcase_31 AC 1,044 ms
182,436 KB
testcase_32 AC 80 ms
71,476 KB
testcase_33 AC 482 ms
172,856 KB
testcase_34 TLE -
権限があれば一括ダウンロードができます

ソースコード

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
        
        while f>0:
            h = [0]*self.N
            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