結果

問題 No.1324 Approximate the Matrix
ユーザー tpynerivertpyneriver
提出日時 2020-12-21 13:37:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,217 ms / 2,000 ms
コード長 2,455 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 82,448 KB
実行使用メモリ 87,852 KB
最終ジャッジ日時 2024-09-21 12:55:45
合計ジャッジ時間 15,515 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
52,872 KB
testcase_01 AC 48 ms
53,600 KB
testcase_02 AC 49 ms
54,128 KB
testcase_03 AC 702 ms
82,712 KB
testcase_04 AC 611 ms
82,860 KB
testcase_05 AC 681 ms
82,564 KB
testcase_06 AC 711 ms
82,836 KB
testcase_07 AC 691 ms
83,196 KB
testcase_08 AC 131 ms
77,440 KB
testcase_09 AC 144 ms
77,632 KB
testcase_10 AC 173 ms
78,168 KB
testcase_11 AC 236 ms
78,416 KB
testcase_12 AC 153 ms
78,048 KB
testcase_13 AC 127 ms
77,664 KB
testcase_14 AC 238 ms
78,708 KB
testcase_15 AC 155 ms
78,000 KB
testcase_16 AC 110 ms
76,784 KB
testcase_17 AC 238 ms
78,776 KB
testcase_18 AC 143 ms
78,124 KB
testcase_19 AC 143 ms
77,988 KB
testcase_20 AC 119 ms
77,432 KB
testcase_21 AC 117 ms
76,472 KB
testcase_22 AC 58 ms
67,920 KB
testcase_23 AC 244 ms
78,088 KB
testcase_24 AC 311 ms
79,916 KB
testcase_25 AC 244 ms
78,484 KB
testcase_26 AC 220 ms
78,804 KB
testcase_27 AC 185 ms
78,256 KB
testcase_28 AC 41 ms
54,548 KB
testcase_29 AC 52 ms
64,000 KB
testcase_30 AC 54 ms
67,148 KB
testcase_31 AC 60 ms
66,972 KB
testcase_32 AC 41 ms
54,256 KB
testcase_33 AC 40 ms
53,272 KB
testcase_34 AC 42 ms
54,192 KB
testcase_35 AC 46 ms
60,100 KB
testcase_36 AC 41 ms
53,016 KB
testcase_37 AC 1,217 ms
87,796 KB
testcase_38 AC 1,184 ms
87,852 KB
testcase_39 AC 1,206 ms
87,776 KB
testcase_40 AC 1,090 ms
87,484 KB
testcase_41 AC 1,194 ms
87,496 KB
testcase_42 AC 56 ms
69,196 KB
testcase_43 AC 56 ms
69,848 KB
testcase_44 AC 57 ms
69,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

from heapq import heappop as hpp, heappush as hp
class MinCostFlowwithDijkstra:
    INF = 1<<60
    
    def __init__(self, N):
        self.N = N
        self.Edge = [[] for _ in range(N)]
    
    def add_edge(self, st, en, cap, cost):
        self.Edge[st].append([en, cap, cost, len(self.Edge[en])])
        self.Edge[en].append([st, 0, -cost, len(self.Edge[st])-1])
    
    def get_mf(self, so, si, fl):
        N = self.N
        INF = self.INF
        res = 0
        Pot = [0]*N
        geta = N
        
        
        prv = [None]*N
        prenum = [None]*N
        while fl:
            dist = [INF]*N
            dist[so] = 0
            Q = [so]
            
            while Q:
                cost, vn = divmod(hpp(Q), geta)
                if dist[vn] < cost:
                    continue
                
                for enum in range(len(self.Edge[vn])):
                    vf, cap, cost, _ = self.Edge[vn][enum]
                    cc = dist[vn] + cost - Pot[vn] + Pot[vf]
                    if cap > 0 and dist[vf] > cc:
                        dist[vf] = cc
                        prv[vf] = vn
                        prenum[vf] = enum
                        hp(Q, cc*geta + vf)
            
            if dist[si] == INF:
                return -1
            
            for i in range(N):
                Pot[i] -= dist[i]
            
            cfl = fl
            vf = si
            while vf != so:
                cfl = min(cfl, self.Edge[prv[vf]][prenum[vf]][1])
                vf = prv[vf]
            
            fl -= cfl
            res -= cfl*Pot[si]
            vf = si
            while vf != so:
                e = self.Edge[prv[vf]][prenum[vf]]
                e[1] -= cfl
                self.Edge[vf][e[3]][1] += cfl
                vf = prv[vf]
        return res
  
N, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

P = [list(map(int, readline().split())) for _ in range(N)]
st = 2*N
en = st + 1
INF = 1000
D = MinCostFlowwithDijkstra(en+1)
Ag = 0
Bg = N
for i in range(N):
    D.add_edge(st, Ag+i, A[i], 0)
    D.add_edge(Bg+i, en, B[i], 0)
    ans = 0
for i in range(N):
    for j in range(N):
        pij = P[i][j]
        ans += pij**2
        for k in range(min(A[i], B[j])):
            D.add_edge(Ag+i, Bg+j, 1, INF -2*(pij-k) + 1)
    
print(ans + D.get_mf(st, en, K) - INF*K)
0