結果

問題 No.1301 Strange Graph Shortest Path
ユーザー hiragnhiragn
提出日時 2022-12-13 14:09:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,617 ms / 3,000 ms
コード長 2,021 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 137,072 KB
最終ジャッジ日時 2024-04-25 00:11:14
合計ジャッジ時間 13,968 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
11,136 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 2,279 ms
124,916 KB
testcase_03 AC 1,765 ms
111,104 KB
testcase_04 AC 2,583 ms
136,952 KB
testcase_05 AC 1,904 ms
122,880 KB
testcase_06 AC 2,424 ms
125,680 KB
testcase_07 AC 2,343 ms
122,880 KB
testcase_08 AC 1,783 ms
111,568 KB
testcase_09 AC 2,262 ms
119,844 KB
testcase_10 AC 1,807 ms
111,864 KB
testcase_11 AC 2,439 ms
127,716 KB
testcase_12 AC 2,451 ms
129,104 KB
testcase_13 AC 2,262 ms
126,256 KB
testcase_14 AC 2,241 ms
118,416 KB
testcase_15 AC 2,217 ms
118,276 KB
testcase_16 AC 2,617 ms
137,072 KB
testcase_17 AC 2,406 ms
129,896 KB
testcase_18 AC 2,170 ms
118,108 KB
testcase_19 AC 2,387 ms
126,240 KB
testcase_20 AC 2,311 ms
126,800 KB
testcase_21 AC 2,352 ms
126,372 KB
testcase_22 AC 2,420 ms
130,268 KB
testcase_23 AC 2,293 ms
126,612 KB
testcase_24 AC 2,421 ms
126,696 KB
testcase_25 AC 2,494 ms
134,456 KB
testcase_26 AC 2,340 ms
124,332 KB
testcase_27 AC 2,379 ms
126,852 KB
testcase_28 AC 1,955 ms
119,268 KB
testcase_29 AC 2,484 ms
136,572 KB
testcase_30 AC 2,500 ms
132,664 KB
testcase_31 AC 2,476 ms
133,608 KB
testcase_32 AC 29 ms
11,136 KB
testcase_33 AC 1,246 ms
120,928 KB
testcase_34 AC 2,492 ms
129,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 = [0] * 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


def main():
    n, m = map(int, input().split())
    
    g = MinCostFlow(n)
    for _ in range(m):
        u, v, c, d = map(int, input().split())
        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.flow(0, n - 1, 2))


if __name__ == "__main__":
    main()
0