結果

問題 No.1301 Strange Graph Shortest Path
ユーザー tamatotamato
提出日時 2020-11-28 01:32:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,075 ms / 3,000 ms
コード長 2,438 bytes
コンパイル時間 459 ms
コンパイル使用メモリ 82,444 KB
実行使用メモリ 198,464 KB
最終ジャッジ日時 2024-09-12 21:40:36
合計ジャッジ時間 33,688 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,648 KB
testcase_01 AC 38 ms
54,360 KB
testcase_02 AC 895 ms
186,864 KB
testcase_03 AC 846 ms
173,800 KB
testcase_04 AC 996 ms
191,308 KB
testcase_05 AC 906 ms
189,736 KB
testcase_06 AC 900 ms
181,848 KB
testcase_07 AC 928 ms
184,072 KB
testcase_08 AC 889 ms
176,012 KB
testcase_09 AC 865 ms
176,968 KB
testcase_10 AC 813 ms
175,232 KB
testcase_11 AC 1,053 ms
185,996 KB
testcase_12 AC 947 ms
186,176 KB
testcase_13 AC 919 ms
188,568 KB
testcase_14 AC 880 ms
177,236 KB
testcase_15 AC 897 ms
178,824 KB
testcase_16 AC 984 ms
191,692 KB
testcase_17 AC 933 ms
190,460 KB
testcase_18 AC 974 ms
179,968 KB
testcase_19 AC 931 ms
183,568 KB
testcase_20 AC 875 ms
180,876 KB
testcase_21 AC 889 ms
187,184 KB
testcase_22 AC 986 ms
184,528 KB
testcase_23 AC 893 ms
188,104 KB
testcase_24 AC 922 ms
182,584 KB
testcase_25 AC 1,010 ms
191,676 KB
testcase_26 AC 923 ms
184,156 KB
testcase_27 AC 945 ms
185,296 KB
testcase_28 AC 788 ms
183,480 KB
testcase_29 AC 940 ms
190,092 KB
testcase_30 AC 1,060 ms
191,028 KB
testcase_31 AC 980 ms
190,000 KB
testcase_32 AC 40 ms
54,244 KB
testcase_33 AC 434 ms
184,028 KB
testcase_34 AC 1,075 ms
198,464 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