結果

問題 No.1678 Coin Trade (Multiple)
ユーザー tamatotamato
提出日時 2021-09-10 22:19:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,607 bytes
コンパイル時間 3,315 ms
コンパイル使用メモリ 82,412 KB
実行使用メモリ 277,580 KB
最終ジャッジ日時 2024-06-12 00:51:51
合計ジャッジ時間 30,115 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
277,580 KB
testcase_01 AC 34 ms
52,864 KB
testcase_02 AC 34 ms
53,248 KB
testcase_03 AC 658 ms
109,360 KB
testcase_04 AC 3,197 ms
179,064 KB
testcase_05 AC 1,193 ms
132,780 KB
testcase_06 AC 944 ms
124,288 KB
testcase_07 AC 3,076 ms
178,784 KB
testcase_08 AC 1,908 ms
148,060 KB
testcase_09 AC 1,244 ms
135,880 KB
testcase_10 AC 712 ms
100,604 KB
testcase_11 AC 2,139 ms
156,776 KB
testcase_12 AC 731 ms
102,272 KB
testcase_13 AC 4,705 ms
228,676 KB
testcase_14 AC 1,745 ms
141,328 KB
testcase_15 AC 1,650 ms
140,936 KB
testcase_16 AC 408 ms
107,612 KB
testcase_17 AC 1,287 ms
136,624 KB
testcase_18 AC 57 ms
66,940 KB
testcase_19 AC 60 ms
54,280 KB
testcase_20 AC 46 ms
64,328 KB
testcase_21 AC 46 ms
63,956 KB
testcase_22 AC 50 ms
66,188 KB
testcase_23 AC 39 ms
59,988 KB
testcase_24 AC 52 ms
67,936 KB
testcase_25 AC 36 ms
54,716 KB
testcase_26 AC 54 ms
66,548 KB
testcase_27 AC 67 ms
62,788 KB
testcase_28 AC 43 ms
63,724 KB
testcase_29 AC 35 ms
55,388 KB
testcase_30 AC 40 ms
61,512 KB
testcase_31 AC 37 ms
60,448 KB
testcase_32 AC 33 ms
54,360 KB
testcase_33 AC 53 ms
69,108 KB
testcase_34 AC 41 ms
62,312 KB
testcase_35 AC 34 ms
54,036 KB
testcase_36 AC 56 ms
68,884 KB
testcase_37 AC 59 ms
71,084 KB
testcase_38 AC 43 ms
63,256 KB
testcase_39 AC 40 ms
61,984 KB
testcase_40 AC 53 ms
68,696 KB
testcase_41 AC 47 ms
64,396 KB
testcase_42 AC 56 ms
71,156 KB
testcase_43 AC 60 ms
71,100 KB
testcase_44 AC 40 ms
60,912 KB
testcase_45 AC 36 ms
53,868 KB
testcase_46 AC 65 ms
71,048 KB
testcase_47 AC 52 ms
66,640 KB
testcase_48 TLE -
testcase_49 TLE -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
権限があれば一括ダウンロードができます

ソースコード

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:
            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