結果

問題 No.1301 Strange Graph Shortest Path
ユーザー rlangevinrlangevin
提出日時 2023-10-20 00:07:23
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,243 bytes
コンパイル時間 2,027 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 191,904 KB
最終ジャッジ日時 2023-10-20 00:08:50
合計ジャッジ時間 30,355 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,612 KB
testcase_01 AC 33 ms
53,612 KB
testcase_02 AC 2,476 ms
182,152 KB
testcase_03 AC 2,741 ms
170,336 KB
testcase_04 TLE -
testcase_05 AC 2,236 ms
185,500 KB
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 1,822 ms
171,624 KB
testcase_09 TLE -
testcase_10 AC 1,826 ms
171,224 KB
testcase_11 AC 2,929 ms
181,624 KB
testcase_12 AC 2,844 ms
182,176 KB
testcase_13 AC 2,839 ms
183,144 KB
testcase_14 AC 2,374 ms
172,792 KB
testcase_15 AC 2,597 ms
173,972 KB
testcase_16 AC 2,758 ms
187,952 KB
testcase_17 TLE -
testcase_18 AC 2,968 ms
175,456 KB
testcase_19 AC 2,509 ms
179,500 KB
testcase_20 AC 2,982 ms
178,108 KB
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 AC 2,679 ms
179,060 KB
testcase_25 AC 2,919 ms
187,052 KB
testcase_26 TLE -
testcase_27 AC 2,722 ms
181,064 KB
testcase_28 AC 2,564 ms
179,748 KB
testcase_29 TLE -
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.inf = 10 ** 18
        self.G = [[] for i 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 bellman_ford(self, s):
        dist = [self.inf] * self.N
        dist[s] = 0
        pv = [0] * self.N
        pe = [0] * self.N
        
        while True:
            update = False
            for v in range(self.N):
                if dist[v] == self.inf:
                    continue
                
                for i in range(len(self.G[v])):
                    next, cap, cost, _ = self.G[v][i]
                    
                    if cap > 0 and dist[next] > dist[v] + cost:
                        dist[next] = dist[v] + cost
                        update = True
                        pv[next] = v
                        pe[next] = i            
            if not update:
                break
            
        return dist, pv, pe
    
    def calc_min_cost_flow(self, s, t, f):
        result = 0
        while f > 0:
            dist, pv, pe = self.bellman_ford(s)
            if dist[t] == self.inf:
                return self.inf
            flow = f
            v = t
            while v != s:
                flow = min(flow, self.G[pv[v]][pe[v]][1])
                v = pv[v]
            result += flow * dist[t]
            f -= flow
            v = t
            while v != s:
                d, cap, cost, r = self.G[pv[v]][pe[v]]
                cap -= flow
                self.G[pv[v]][pe[v]] = (d, cap, cost, r)
                rev = self.G[pv[v]][pe[v]][3]
                d, cap, cost, r = self.G[v][rev]
                cap += flow
                self.G[v][rev] = (d, cap, cost, r)
                v = pv[v]
                
        return result
    
    
N, M = map(int, input().split())
G = MinCostFlow(N)
for i in range(M):
    u, v, c, d = map(int, input().split())
    u, v = 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.calc_min_cost_flow(0, N-1, 2))
0