結果

問題 No.1678 Coin Trade (Multiple)
ユーザー tamatotamato
提出日時 2021-09-10 22:19:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,607 bytes
コンパイル時間 1,465 ms
コンパイル使用メモリ 87,524 KB
実行使用メモリ 266,708 KB
最終ジャッジ日時 2023-09-02 18:24:03
合計ジャッジ時間 43,174 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,572 KB
testcase_01 AC 75 ms
71,788 KB
testcase_02 AC 75 ms
71,572 KB
testcase_03 AC 758 ms
110,208 KB
testcase_04 AC 3,564 ms
180,984 KB
testcase_05 AC 1,393 ms
134,820 KB
testcase_06 AC 1,111 ms
126,824 KB
testcase_07 AC 3,466 ms
180,160 KB
testcase_08 AC 2,217 ms
148,460 KB
testcase_09 AC 1,465 ms
137,160 KB
testcase_10 AC 967 ms
100,416 KB
testcase_11 AC 2,505 ms
158,240 KB
testcase_12 AC 975 ms
104,004 KB
testcase_13 TLE -
testcase_14 AC 2,038 ms
141,408 KB
testcase_15 AC 1,906 ms
144,196 KB
testcase_16 AC 492 ms
108,716 KB
testcase_17 AC 1,499 ms
138,012 KB
testcase_18 AC 95 ms
76,504 KB
testcase_19 AC 76 ms
71,620 KB
testcase_20 AC 87 ms
76,776 KB
testcase_21 AC 84 ms
76,100 KB
testcase_22 AC 89 ms
76,784 KB
testcase_23 AC 81 ms
75,796 KB
testcase_24 AC 96 ms
76,788 KB
testcase_25 AC 79 ms
71,572 KB
testcase_26 AC 93 ms
76,796 KB
testcase_27 AC 86 ms
75,948 KB
testcase_28 AC 85 ms
76,216 KB
testcase_29 AC 78 ms
71,676 KB
testcase_30 AC 83 ms
75,696 KB
testcase_31 AC 79 ms
75,808 KB
testcase_32 AC 76 ms
71,784 KB
testcase_33 AC 95 ms
76,212 KB
testcase_34 AC 80 ms
76,132 KB
testcase_35 AC 74 ms
71,808 KB
testcase_36 AC 98 ms
76,544 KB
testcase_37 AC 100 ms
77,272 KB
testcase_38 AC 81 ms
76,256 KB
testcase_39 AC 80 ms
75,728 KB
testcase_40 AC 94 ms
76,384 KB
testcase_41 AC 90 ms
76,568 KB
testcase_42 AC 101 ms
76,584 KB
testcase_43 AC 103 ms
77,656 KB
testcase_44 AC 80 ms
75,908 KB
testcase_45 AC 75 ms
71,492 KB
testcase_46 AC 107 ms
77,484 KB
testcase_47 AC 90 ms
76,704 KB
testcase_48 TLE -
testcase_49 -- -
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