結果

問題 No.1301 Strange Graph Shortest Path
ユーザー roarisroaris
提出日時 2020-11-28 02:18:42
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,928 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 86,700 KB
実行使用メモリ 130,288 KB
最終ジャッジ日時 2023-10-09 23:20:53
合計ジャッジ時間 12,413 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
70,992 KB
testcase_01 AC 64 ms
71,116 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 61 ms
71,040 KB
testcase_33 AC 289 ms
127,584 KB
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

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(u-1, v-1, 1, d)

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