結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,060 KB
testcase_01 AC 76 ms
71,220 KB
testcase_02 AC 77 ms
70,996 KB
testcase_03 AC 748 ms
86,556 KB
testcase_04 AC 699 ms
86,028 KB
testcase_05 AC 777 ms
86,372 KB
testcase_06 AC 738 ms
86,388 KB
testcase_07 AC 679 ms
85,996 KB
testcase_08 AC 192 ms
80,284 KB
testcase_09 AC 217 ms
80,108 KB
testcase_10 AC 245 ms
80,160 KB
testcase_11 AC 304 ms
82,340 KB
testcase_12 AC 218 ms
80,740 KB
testcase_13 AC 190 ms
79,708 KB
testcase_14 AC 303 ms
82,352 KB
testcase_15 AC 224 ms
80,492 KB
testcase_16 AC 165 ms
78,304 KB
testcase_17 AC 303 ms
81,536 KB
testcase_18 AC 221 ms
80,484 KB
testcase_19 AC 214 ms
79,628 KB
testcase_20 AC 179 ms
79,208 KB
testcase_21 AC 174 ms
78,196 KB
testcase_22 AC 95 ms
76,668 KB
testcase_23 AC 289 ms
80,932 KB
testcase_24 AC 382 ms
83,372 KB
testcase_25 AC 331 ms
82,300 KB
testcase_26 AC 296 ms
81,760 KB
testcase_27 AC 233 ms
80,140 KB
testcase_28 AC 74 ms
71,000 KB
testcase_29 AC 86 ms
75,940 KB
testcase_30 AC 85 ms
75,864 KB
testcase_31 AC 93 ms
76,260 KB
testcase_32 AC 74 ms
71,184 KB
testcase_33 AC 75 ms
71,248 KB
testcase_34 AC 77 ms
71,044 KB
testcase_35 AC 74 ms
71,364 KB
testcase_36 AC 75 ms
71,252 KB
testcase_37 AC 1,268 ms
91,396 KB
testcase_38 AC 1,260 ms
91,440 KB
testcase_39 AC 1,108 ms
91,752 KB
testcase_40 AC 1,244 ms
91,648 KB
testcase_41 AC 1,212 ms
91,548 KB
testcase_42 AC 96 ms
77,440 KB
testcase_43 AC 96 ms
77,512 KB
testcase_44 AC 96 ms
77,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#その3 ベルマンフォード+ダイクストラ
from heapq import heappop,heappush
class MinCostFlow:
    inf = 10 ** 18

    def __init__(self,N):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.H = [0] * N
        self.first = True
        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)
        return len(self.edge) - 1
    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
        prev_v = [0] * N
        prev_e = [None] * N

        d0 = [inf] * N
        dist = [inf] * N

        if self.first:
            self.first = False
            dist[:] = d0
            dist[s] = 0
            update = 1
            while update:
                update = 0
                for v in range(N):
                    if dist[v] == inf:continue
                    for e in G[v]:
                        w,cap,cost,_ = e
                        if cap > 0 and dist[v] + cost < dist[w]:
                            dist[w] = dist[v] + cost
                            prev_v[w] = v
                            prev_e[w] = e
                            update = 1
            H[:] = dist
            if dist[t] == inf:
                return None
            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]
        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

N,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
P = [list(map(int,input().split())) for _ in range(N)]

mincost = MinCostFlow(N + N + 2)
s = N + N
t = N + N + 1
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,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)
print(ans)
0