結果

問題 No.1301 Strange Graph Shortest Path
ユーザー nok0nok0
提出日時 2020-10-30 22:24:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,896 ms / 3,000 ms
コード長 2,005 bytes
コンパイル時間 491 ms
コンパイル使用メモリ 87,116 KB
実行使用メモリ 262,340 KB
最終ジャッジ日時 2023-10-10 21:30:15
合計ジャッジ時間 57,992 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,552 KB
testcase_01 AC 77 ms
71,496 KB
testcase_02 AC 1,787 ms
248,296 KB
testcase_03 AC 1,620 ms
229,880 KB
testcase_04 AC 1,822 ms
252,148 KB
testcase_05 AC 1,719 ms
250,340 KB
testcase_06 AC 1,686 ms
239,156 KB
testcase_07 AC 1,767 ms
243,184 KB
testcase_08 AC 1,635 ms
231,036 KB
testcase_09 AC 1,644 ms
230,204 KB
testcase_10 AC 1,613 ms
230,412 KB
testcase_11 AC 1,842 ms
245,772 KB
testcase_12 AC 1,776 ms
244,352 KB
testcase_13 AC 1,810 ms
250,240 KB
testcase_14 AC 1,635 ms
230,700 KB
testcase_15 AC 1,688 ms
233,940 KB
testcase_16 AC 1,811 ms
251,804 KB
testcase_17 AC 1,864 ms
252,760 KB
testcase_18 AC 1,746 ms
236,784 KB
testcase_19 AC 1,736 ms
240,032 KB
testcase_20 AC 1,659 ms
235,768 KB
testcase_21 AC 1,783 ms
247,872 KB
testcase_22 AC 1,670 ms
239,796 KB
testcase_23 AC 1,845 ms
249,472 KB
testcase_24 AC 1,704 ms
238,832 KB
testcase_25 AC 1,853 ms
253,140 KB
testcase_26 AC 1,770 ms
241,920 KB
testcase_27 AC 1,784 ms
244,760 KB
testcase_28 AC 1,702 ms
242,616 KB
testcase_29 AC 1,804 ms
249,864 KB
testcase_30 AC 1,870 ms
251,256 KB
testcase_31 AC 1,820 ms
249,800 KB
testcase_32 AC 76 ms
71,368 KB
testcase_33 AC 818 ms
240,184 KB
testcase_34 AC 1,896 ms
262,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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):
        G = self.G
        G[fr].append([to, cap, cost, len(G[to])])
        G[to].append([fr, 0, -cost, len(G[fr])-1])
 
    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 = [0]*N
 
        while f:
            dist = [INF]*N
            dist[s] = 0
            que = [(0, s)]
 
            while que:
                c, v = heappop(que)
                if dist[v] < c:
                    continue
                for i, (w, cap, cost, _) in enumerate(G[v]):
                    if cap > 0 and dist[w] > dist[v] + cost + H[v] - H[w]:
                        dist[w] = r = dist[v] + cost + H[v] - H[w]
                        prv_v[w] = v; prv_e[w] = i
                        heappush(que, (r, w))
            if dist[t] == INF:
                return -1
 
            for i in range(N):
                H[i] += dist[i]
 
            d = f; v = t
            while v != s:
                d = min(d, G[prv_v[v]][prv_e[v]][1])
                v = prv_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = G[prv_v[v]][prv_e[v]]
                e[1] -= d
                G[v][e[3]][1] += d
                v = prv_v[v]
        return res

n, m = map(int, input().split())
s, t, w = 0, n - 1, n
G = MinCostFlow(n + 2 * m)

def add(u, v, c, d):
    global w
    G.add_edge(u, w, 2, c)
    G.add_edge(w, v, 1, 0)
    G.add_edge(w, v, 1, d - c)
    w += 1

for i in range(m):
    u, v, c, d = map(int, input().split())
    u -= 1
    v -= 1
    add(u, v, c, d)
    add(v, u, c, d)

res = G.flow(s,t,2)

print(res)
0