結果

問題 No.1324 Approximate the Matrix
ユーザー tamatotamato
提出日時 2021-02-22 17:25:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,516 ms / 2,000 ms
コード長 2,930 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 88,996 KB
最終ジャッジ日時 2024-09-21 13:48:24
合計ジャッジ時間 18,316 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
52,992 KB
testcase_01 AC 44 ms
53,120 KB
testcase_02 AC 44 ms
53,120 KB
testcase_03 AC 750 ms
83,708 KB
testcase_04 AC 633 ms
83,400 KB
testcase_05 AC 756 ms
83,480 KB
testcase_06 AC 734 ms
83,496 KB
testcase_07 AC 746 ms
84,020 KB
testcase_08 AC 160 ms
78,364 KB
testcase_09 AC 199 ms
78,800 KB
testcase_10 AC 239 ms
79,296 KB
testcase_11 AC 295 ms
80,548 KB
testcase_12 AC 203 ms
78,560 KB
testcase_13 AC 164 ms
78,584 KB
testcase_14 AC 305 ms
80,456 KB
testcase_15 AC 194 ms
78,592 KB
testcase_16 AC 141 ms
77,308 KB
testcase_17 AC 304 ms
80,176 KB
testcase_18 AC 194 ms
78,780 KB
testcase_19 AC 189 ms
78,264 KB
testcase_20 AC 162 ms
78,252 KB
testcase_21 AC 167 ms
77,756 KB
testcase_22 AC 73 ms
66,816 KB
testcase_23 AC 276 ms
79,816 KB
testcase_24 AC 378 ms
81,116 KB
testcase_25 AC 311 ms
80,344 KB
testcase_26 AC 307 ms
80,556 KB
testcase_27 AC 222 ms
79,204 KB
testcase_28 AC 49 ms
52,992 KB
testcase_29 AC 63 ms
62,464 KB
testcase_30 AC 59 ms
63,744 KB
testcase_31 AC 74 ms
65,024 KB
testcase_32 AC 48 ms
53,248 KB
testcase_33 AC 44 ms
53,504 KB
testcase_34 AC 46 ms
53,632 KB
testcase_35 AC 46 ms
53,760 KB
testcase_36 AC 44 ms
53,504 KB
testcase_37 AC 1,495 ms
88,820 KB
testcase_38 AC 1,516 ms
88,996 KB
testcase_39 AC 1,332 ms
88,876 KB
testcase_40 AC 1,465 ms
88,372 KB
testcase_41 AC 1,446 ms
88,552 KB
testcase_42 AC 64 ms
68,480 KB
testcase_43 AC 63 ms
68,352 KB
testcase_44 AC 63 ms
68,224 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]
            if cap == 0:
                return
            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())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    P = []
    for _ in range(N):
        P.append(list(map(int, input().split())))

    s = N*2 + 1
    t = s + 1
    mcf = Mincostflow(t)
    for i, a in enumerate(A):
        va = i+1
        mcf.add_edge(s, va, a, 0)
        for j, b in enumerate(B):
            vb = N+j+1
            p = P[i][j]
            for f in range(min(a, b)):
                mcf.add_edge(va, vb, 1, 500 - 2 * (p-f) + 1)
    for j, b in enumerate(B):
        vb = N+j+1
        mcf.add_edge(vb, t, b, 0)
    ans = mcf.flow(s, t, K) - 500 * K
    for i in range(N):
        for j in range(N):
            ans += P[i][j] ** 2
    print(ans)


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