結果

問題 No.1301 Strange Graph Shortest Path
ユーザー roarisroaris
提出日時 2020-11-28 02:20:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,994 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 176,724 KB
最終ジャッジ日時 2023-10-09 23:21:30
合計ジャッジ時間 18,792 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,440 KB
testcase_01 AC 73 ms
70,960 KB
testcase_02 AC 2,901 ms
171,396 KB
testcase_03 AC 2,921 ms
161,316 KB
testcase_04 TLE -
testcase_05 AC 2,581 ms
174,032 KB
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 2,118 ms
162,052 KB
testcase_09 TLE -
testcase_10 AC 2,481 ms
162,104 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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:
            dist = [10**18]*self.N
            dist[s] = 0
            prev_v = [-1]*self.N
            prev_e = [-1]*self.N
            update = True
            
            while update:
                update = False
                
                for v in range(self.N):
                    if dist[v]==10**18:
                        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:
                            dist[nv] = dist[v]+cost
                            prev_v[nv] = v
                            prev_e[nv] = i
                            update = True
            
            if dist[t]==10**18:
                return -1
            
            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*dist[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