結果

問題 No.1301 Strange Graph Shortest Path
ユーザー chineristACchineristAC
提出日時 2020-11-27 21:25:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,303 ms / 3,000 ms
コード長 1,892 bytes
コンパイル時間 430 ms
コンパイル使用メモリ 87,292 KB
実行使用メモリ 211,980 KB
最終ジャッジ日時 2023-10-10 21:39:03
合計ジャッジ時間 35,324 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
72,376 KB
testcase_01 AC 91 ms
72,200 KB
testcase_02 AC 1,016 ms
203,496 KB
testcase_03 AC 930 ms
187,820 KB
testcase_04 AC 1,113 ms
207,688 KB
testcase_05 AC 978 ms
205,172 KB
testcase_06 AC 1,010 ms
197,272 KB
testcase_07 AC 1,001 ms
198,792 KB
testcase_08 AC 957 ms
189,888 KB
testcase_09 AC 948 ms
191,076 KB
testcase_10 AC 911 ms
189,076 KB
testcase_11 AC 1,303 ms
201,452 KB
testcase_12 AC 1,149 ms
202,200 KB
testcase_13 AC 960 ms
203,828 KB
testcase_14 AC 924 ms
190,936 KB
testcase_15 AC 962 ms
192,508 KB
testcase_16 AC 1,086 ms
207,780 KB
testcase_17 AC 1,032 ms
205,932 KB
testcase_18 AC 1,089 ms
195,404 KB
testcase_19 AC 1,025 ms
197,924 KB
testcase_20 AC 984 ms
196,496 KB
testcase_21 AC 1,036 ms
202,284 KB
testcase_22 AC 1,083 ms
198,704 KB
testcase_23 AC 978 ms
203,928 KB
testcase_24 AC 989 ms
198,180 KB
testcase_25 AC 1,101 ms
207,128 KB
testcase_26 AC 974 ms
198,312 KB
testcase_27 AC 1,036 ms
200,988 KB
testcase_28 AC 874 ms
198,432 KB
testcase_29 AC 1,027 ms
205,484 KB
testcase_30 AC 1,099 ms
206,088 KB
testcase_31 AC 1,034 ms
205,728 KB
testcase_32 AC 91 ms
72,204 KB
testcase_33 AC 504 ms
196,224 KB
testcase_34 AC 1,148 ms
211,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random

from heapq import heappush, heappop
class MinCostFlow:
    INF = 10**18

    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap, cost):
        forward = [to, cap, cost, None]
        backward = forward[3] = [fr, 0, -cost, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def flow(self, s, t, f):
        N = self.N; G = self.G
        INF = MinCostFlow.INF

        res = 0
        H = [0]*N
        prv_v = [0]*N
        prv_e = [None]*N

        d0 = [INF]*N
        dist = [INF]*N

        while f:
            dist[:] = d0
            dist[s] = 0
            que = [(0, s)]

            while que:
                c, v = heappop(que)
                if dist[v] < c:
                    continue
                r0 = dist[v] + H[v]
                for e in G[v]:
                    w, cap, cost, _ = e
                    if cap > 0 and r0 + cost - H[w] < dist[w]:
                        dist[w] = r = r0 + cost - H[w]
                        prv_v[w] = v; prv_e[w] = e
                        heappush(que, (r, w))
            if dist[t] == INF:
                return None

            for i in range(N):
                H[i] += dist[i]

            d = f; v = t
            while v != s:
                d = min(d, prv_e[v][1])
                v = prv_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = prv_e[v]
                e[1] -= d
                e[3][1] += d
                v = prv_v[v]
        return res

import sys

input = sys.stdin.readline

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

print(G.flow(0,N-1,2))
0