結果

問題 No.1301 Strange Graph Shortest Path
ユーザー 👑 tamatotamato
提出日時 2020-11-28 01:32:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,091 ms / 3,000 ms
コード長 2,438 bytes
コンパイル時間 805 ms
コンパイル使用メモリ 87,816 KB
実行使用メモリ 201,404 KB
最終ジャッジ日時 2023-10-09 23:13:54
合計ジャッジ時間 34,705 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,580 KB
testcase_01 AC 74 ms
71,804 KB
testcase_02 AC 946 ms
188,524 KB
testcase_03 AC 878 ms
176,248 KB
testcase_04 AC 1,043 ms
193,584 KB
testcase_05 AC 922 ms
190,944 KB
testcase_06 AC 932 ms
183,640 KB
testcase_07 AC 943 ms
185,388 KB
testcase_08 AC 886 ms
177,424 KB
testcase_09 AC 883 ms
178,424 KB
testcase_10 AC 846 ms
176,720 KB
testcase_11 AC 1,091 ms
188,880 KB
testcase_12 AC 970 ms
188,024 KB
testcase_13 AC 940 ms
189,948 KB
testcase_14 AC 913 ms
178,776 KB
testcase_15 AC 915 ms
180,612 KB
testcase_16 AC 997 ms
193,240 KB
testcase_17 AC 949 ms
191,992 KB
testcase_18 AC 1,000 ms
182,292 KB
testcase_19 AC 966 ms
184,712 KB
testcase_20 AC 926 ms
182,576 KB
testcase_21 AC 929 ms
189,008 KB
testcase_22 AC 1,012 ms
186,428 KB
testcase_23 AC 923 ms
190,516 KB
testcase_24 AC 941 ms
183,476 KB
testcase_25 AC 1,057 ms
192,868 KB
testcase_26 AC 920 ms
185,708 KB
testcase_27 AC 976 ms
187,216 KB
testcase_28 AC 823 ms
184,984 KB
testcase_29 AC 961 ms
191,880 KB
testcase_30 AC 1,088 ms
193,500 KB
testcase_31 AC 1,022 ms
191,896 KB
testcase_32 AC 77 ms
71,448 KB
testcase_33 AC 470 ms
186,020 KB
testcase_34 AC 1,068 ms
201,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.buffer.readline
    from heapq import heappush, heappop

    class Mincostflow():
        def __init__(self, N):
            self.N = N
            self.adj = [[] for _ in range(N + 1)]
            self.inf = 1 << 60

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

        def flow(self, s, t, f):
            N = self.N
            adj = self.adj
            inf = self.inf

            res = 0
            H = [0] * (N + 1)
            prev_v = [0] * (N + 1)
            prev_e = [None] * (N + 1)

            dist0 = [inf] * (N + 1)
            dist = [inf] * (N + 1)

            while f:
                dist[:] = dist0
                dist[s] = 0
                pq = [(0, s)]
                while pq:
                    d, v = heappop(pq)
                    if d > dist[v]:
                        continue
                    r0 = dist[v] + H[v]
                    for e in adj[v]:
                        u, cap, cost, _ = e
                        if cap > 0 and r0 + cost - H[u] < dist[u]:
                            dist[u] = r = r0 + cost - H[u]
                            heappush(pq, (r, u))
                            prev_v[u] = v
                            prev_e[u] = e

                # flow f doesn't exist
                if dist[t] == inf:
                    return None

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

                g = f
                v = t
                while v != s:
                    g = min(g, prev_e[v][1])
                    v = prev_v[v]
                f -= g
                res += g * H[t]
                v = t
                while v != s:
                    e = prev_e[v]
                    e[1] -= g
                    e[-1][1] += g
                    v = prev_v[v]

            return res

    N, M = map(int, input().split())
    mcf = Mincostflow(N)
    for _ in range(M):
        a, b, c, d = map(int, input().split())
        mcf.add_edge(a, b, 1, c)
        mcf.add_edge(b, a, 1, c)
        mcf.add_edge(a, b, 1, d)
        mcf.add_edge(b, a, 1, d)
    print(mcf.flow(1, N, 2))


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