結果

問題 No.1678 Coin Trade (Multiple)
ユーザー 👑 tamatotamato
提出日時 2021-09-10 22:22:27
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 4,812 ms / 5,000 ms
コード長 2,640 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 87,596 KB
実行使用メモリ 243,712 KB
最終ジャッジ日時 2023-09-02 18:32:02
合計ジャッジ時間 75,360 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,652 KB
testcase_01 AC 74 ms
71,732 KB
testcase_02 AC 74 ms
71,880 KB
testcase_03 AC 551 ms
97,876 KB
testcase_04 AC 2,288 ms
141,464 KB
testcase_05 AC 971 ms
117,736 KB
testcase_06 AC 739 ms
112,364 KB
testcase_07 AC 2,110 ms
134,644 KB
testcase_08 AC 1,356 ms
120,036 KB
testcase_09 AC 1,002 ms
119,420 KB
testcase_10 AC 799 ms
90,796 KB
testcase_11 AC 1,695 ms
128,280 KB
testcase_12 AC 628 ms
91,888 KB
testcase_13 AC 3,727 ms
170,148 KB
testcase_14 AC 1,241 ms
112,376 KB
testcase_15 AC 1,347 ms
119,288 KB
testcase_16 AC 435 ms
100,460 KB
testcase_17 AC 1,082 ms
120,284 KB
testcase_18 AC 88 ms
76,844 KB
testcase_19 AC 76 ms
71,560 KB
testcase_20 AC 78 ms
71,784 KB
testcase_21 AC 85 ms
76,576 KB
testcase_22 AC 89 ms
76,724 KB
testcase_23 AC 77 ms
71,804 KB
testcase_24 AC 88 ms
76,504 KB
testcase_25 AC 77 ms
71,752 KB
testcase_26 AC 85 ms
76,392 KB
testcase_27 AC 78 ms
71,784 KB
testcase_28 AC 81 ms
75,780 KB
testcase_29 AC 78 ms
71,748 KB
testcase_30 AC 85 ms
76,620 KB
testcase_31 AC 77 ms
71,516 KB
testcase_32 AC 78 ms
71,756 KB
testcase_33 AC 96 ms
76,540 KB
testcase_34 AC 82 ms
76,100 KB
testcase_35 AC 73 ms
71,732 KB
testcase_36 AC 95 ms
76,420 KB
testcase_37 AC 97 ms
76,564 KB
testcase_38 AC 78 ms
71,664 KB
testcase_39 AC 78 ms
71,736 KB
testcase_40 AC 90 ms
76,620 KB
testcase_41 AC 82 ms
76,060 KB
testcase_42 AC 92 ms
76,360 KB
testcase_43 AC 98 ms
76,588 KB
testcase_44 AC 78 ms
71,676 KB
testcase_45 AC 76 ms
71,876 KB
testcase_46 AC 93 ms
76,644 KB
testcase_47 AC 82 ms
75,972 KB
testcase_48 AC 4,464 ms
192,040 KB
testcase_49 AC 4,551 ms
190,320 KB
testcase_50 AC 4,531 ms
191,512 KB
testcase_51 AC 4,528 ms
192,972 KB
testcase_52 AC 4,558 ms
191,540 KB
testcase_53 AC 4,571 ms
193,844 KB
testcase_54 AC 4,812 ms
193,988 KB
testcase_55 AC 4,602 ms
190,740 KB
testcase_56 AC 4,661 ms
192,348 KB
testcase_57 AC 4,550 ms
193,828 KB
testcase_58 AC 3,359 ms
243,712 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, K = map(int, input().split())
    mcf = Mincostflow(N + 2)
    s = N + 2
    t = N + 1
    L = 10 ** 10
    mcf.add_edge(s, 1, K, L)
    A = [0] * (N+1)
    for i in range(1, N+1):
        a, M = map(int, input().split())
        A[i] = a
        B = list(map(int, input().split()))
        mcf.add_edge(i, i+1, K, L)
        for b in B:
            if a - A[b] > 0:
                mcf.add_edge(b, i, 1, L * (i - b) - (a - A[b]))
    f = mcf.flow(s, t, K)
    print(L * (N + 1) * K - f)


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