結果

問題 No.1324 Approximate the Matrix
ユーザー ああいいああいい
提出日時 2022-04-14 10:22:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,284 ms / 2,000 ms
コード長 2,618 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 91,924 KB
最終ジャッジ日時 2023-08-25 19:48:40
合計ジャッジ時間 18,203 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,356 KB
testcase_01 AC 74 ms
71,580 KB
testcase_02 AC 76 ms
71,556 KB
testcase_03 AC 755 ms
86,272 KB
testcase_04 AC 711 ms
85,616 KB
testcase_05 AC 731 ms
85,244 KB
testcase_06 AC 726 ms
86,032 KB
testcase_07 AC 679 ms
86,396 KB
testcase_08 AC 181 ms
79,960 KB
testcase_09 AC 209 ms
80,092 KB
testcase_10 AC 234 ms
80,384 KB
testcase_11 AC 294 ms
81,944 KB
testcase_12 AC 220 ms
79,964 KB
testcase_13 AC 190 ms
79,668 KB
testcase_14 AC 293 ms
81,716 KB
testcase_15 AC 221 ms
80,236 KB
testcase_16 AC 162 ms
78,128 KB
testcase_17 AC 303 ms
81,716 KB
testcase_18 AC 202 ms
80,232 KB
testcase_19 AC 209 ms
79,600 KB
testcase_20 AC 173 ms
78,688 KB
testcase_21 AC 184 ms
78,732 KB
testcase_22 AC 94 ms
76,632 KB
testcase_23 AC 297 ms
81,156 KB
testcase_24 AC 372 ms
82,116 KB
testcase_25 AC 328 ms
82,120 KB
testcase_26 AC 284 ms
80,764 KB
testcase_27 AC 232 ms
80,604 KB
testcase_28 AC 74 ms
71,292 KB
testcase_29 AC 85 ms
76,044 KB
testcase_30 AC 84 ms
75,836 KB
testcase_31 AC 91 ms
76,396 KB
testcase_32 AC 75 ms
71,508 KB
testcase_33 AC 75 ms
71,220 KB
testcase_34 AC 77 ms
71,352 KB
testcase_35 AC 80 ms
75,216 KB
testcase_36 AC 76 ms
71,264 KB
testcase_37 AC 1,284 ms
90,916 KB
testcase_38 AC 1,248 ms
91,924 KB
testcase_39 AC 1,085 ms
91,492 KB
testcase_40 AC 1,237 ms
90,840 KB
testcase_41 AC 1,279 ms
91,196 KB
testcase_42 AC 91 ms
76,512 KB
testcase_43 AC 89 ms
76,600 KB
testcase_44 AC 90 ms
76,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#その2 最初はコストが全部正の場合に使えるダイクストラ
from heapq import heappush,heappop
class MinCostFlow:
    inf = 10 ** 18

    def __init__(self,N):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.H = [0] * N
        self.edge = []

    def add_edge(self,fr,to,cap,cost):
        e = [to,cap,cost,None]
        r = e[3] = [fr,0,-cost,e]
        self.G[fr].append(e)
        self.G[to].append(r)
        self.edge.append(e)
    def get_edge(self,i):
        return self.edge[i]

    def flow(self,s,t,f):
        N = self.N
        G = self.G
        inf = MinCostFlow.inf

        res = 0
        H = self.H  #ポテンシャル、コストが最初は正なので、最初は0でいい
        prev_v = [0] * N
        prev_e = [None] * N

        d0 = [inf] * N
        dist = [inf] * N
        while f:
            dist[:] = d0
            dist[s] = 0
            q = [(0,s)]
            while q:
                c,v = heappop(q)
                if dist[v] < c:continue
                r0 = dist[v] + H[v]
                for e in G[v]:
                    w,cap,cost,_ = e
                    if cap > 0 and r0 + cost - H[w] < dist[w]:
                        dist[w] = r = r0 + cost - H[w]
                        prev_v[w] = v
                        prev_e[w] = e
                        heappush(q,(r,w))
            if dist[t] == inf:
                return None
            """
            for i in range(N):
                H[i] += dist[i]
            """
            H = [h + d for h,d in zip(H,dist)]
            d = f
            v = t
            while v != s:
                d = min(d,prev_e[v][1])
                v = prev_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = prev_e[v]
                e[1] -= d
                e[3][1] += d
                v = prev_v[v]
        return res
import sys
rr = sys.stdin
N,K = map(int,rr.readline().split())
A = list(map(int,rr.readline().split()))
B = list(map(int,rr.readline().split()))
P = [list(map(int,rr.readline().split())) for _ in range(N)]

mincost = MinCostFlow(N + N + 2)
s = N + N
t = N + N + 1
inf = 400
for i in range(N):
    for j in range(N):
        v = N + j
        for k in range(1,min(A[i],B[j])+1):
            mincost.add_edge(i,v,1,inf + (2 * (k - 1) + 1 - 2 * P[i][j]))
for i in range(N):
    mincost.add_edge(s,i,A[i],0)
for j in range(N):
    mincost.add_edge(j + N,t,B[j],0)
ans = 0
for i in range(N):
    for j in range(N):
        ans += P[i][j] ** 2
ans += mincost.flow(s,t,K) - inf * K
print(ans)
0