結果

問題 No.1324 Approximate the Matrix
ユーザー roarisroaris
提出日時 2020-12-23 22:56:34
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 872 ms / 2,000 ms
コード長 2,360 bytes
コンパイル時間 690 ms
コンパイル使用メモリ 81,840 KB
実行使用メモリ 88,604 KB
最終ジャッジ日時 2023-10-21 15:21:29
合計ジャッジ時間 15,738 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,584 KB
testcase_01 AC 33 ms
53,584 KB
testcase_02 AC 34 ms
53,584 KB
testcase_03 AC 832 ms
88,500 KB
testcase_04 AC 830 ms
88,000 KB
testcase_05 AC 838 ms
88,176 KB
testcase_06 AC 855 ms
88,448 KB
testcase_07 AC 857 ms
88,172 KB
testcase_08 AC 173 ms
79,184 KB
testcase_09 AC 178 ms
79,248 KB
testcase_10 AC 210 ms
79,688 KB
testcase_11 AC 285 ms
81,008 KB
testcase_12 AC 172 ms
78,312 KB
testcase_13 AC 148 ms
78,188 KB
testcase_14 AC 307 ms
82,068 KB
testcase_15 AC 187 ms
79,928 KB
testcase_16 AC 109 ms
77,124 KB
testcase_17 AC 247 ms
79,604 KB
testcase_18 AC 175 ms
78,260 KB
testcase_19 AC 167 ms
78,220 KB
testcase_20 AC 142 ms
78,096 KB
testcase_21 AC 136 ms
77,740 KB
testcase_22 AC 98 ms
77,536 KB
testcase_23 AC 218 ms
79,216 KB
testcase_24 AC 383 ms
82,452 KB
testcase_25 AC 261 ms
79,984 KB
testcase_26 AC 244 ms
80,184 KB
testcase_27 AC 196 ms
78,632 KB
testcase_28 AC 32 ms
53,584 KB
testcase_29 AC 42 ms
64,044 KB
testcase_30 AC 49 ms
66,192 KB
testcase_31 AC 54 ms
68,460 KB
testcase_32 AC 37 ms
53,584 KB
testcase_33 AC 36 ms
53,584 KB
testcase_34 AC 37 ms
53,584 KB
testcase_35 AC 39 ms
61,412 KB
testcase_36 AC 40 ms
62,084 KB
testcase_37 AC 832 ms
88,604 KB
testcase_38 AC 861 ms
88,240 KB
testcase_39 AC 828 ms
88,052 KB
testcase_40 AC 872 ms
88,132 KB
testcase_41 AC 857 ms
88,364 KB
testcase_42 AC 48 ms
70,704 KB
testcase_43 AC 49 ms
70,700 KB
testcase_44 AC 50 ms
70,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from heapq import *

class Mincostflow:
    def __init__(self, N):
        self.N = N
        self.G = [[] for _ in range(N)]
        
    def add_edge(self, u, v, cap, cost):
        self.G[u].append([v, cap, cost, len(self.G[v])])
        self.G[v].append([u, 0, -cost, len(self.G[u])-1])
    
    def min_cost_flow(self, s, t, f):
        res = 0
        h = [0]*self.N
        
        while f>0:
            dist = [10**18]*self.N
            dist[s] = 0
            pq = [(0, s)]
            prev_v = [-1]*self.N
            prev_e = [-1]*self.N
            
            while pq:
                d, v = heappop(pq)
                
                if dist[v]<d:
                    continue
                
                for i in range(len(self.G[v])):
                    nv, cap, cost, _  = self.G[v][i]
                    
                    if cap>0 and dist[nv]>dist[v]+cost+h[v]-h[nv]:
                        dist[nv] = dist[v]+cost+h[v]-h[nv]
                        prev_v[nv] = v
                        prev_e[nv] = i
                        heappush(pq, (dist[nv], nv))
            
            if dist[t]==10**18:
                return -1
            
            for i in range(self.N):
                h[i] += dist[i]
                
            d = f
            v = t
            
            while v!=s:
                d = min(d, self.G[prev_v[v]][prev_e[v]][1])
                v = prev_v[v]
            
            f -= d
            res += d*h[t]
            v = t
            
            while v!=s:
                self.G[prev_v[v]][prev_e[v]][1] -= d
                rev = self.G[prev_v[v]][prev_e[v]][3]
                self.G[v][rev][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)]
mcf = Mincostflow(2*N+2)

for i in range(1, N+1):
    mcf.add_edge(0, i, A[i-1], 0)

for i in range(N+1, 2*N+1):
    mcf.add_edge(i, 2*N+1, B[i-N-1], 0)

for i in range(N):
    for j in range(N):
        for k in range(A[i]):
            mcf.add_edge(i+1, N+1+j, 1, (k+1-P[i][j])**2-(k-P[i][j])**2)

ans = mcf.min_cost_flow(0, 2*N+1, K)

for i in range(N):
    for j in range(N):
        ans += P[i][j]**2

print(ans)
0