結果
問題 | No.1324 Approximate the Matrix |
ユーザー | tpyneriver |
提出日時 | 2020-12-21 13:37:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,273 ms / 2,000 ms |
コード長 | 2,455 bytes |
コンパイル時間 | 319 ms |
コンパイル使用メモリ | 81,576 KB |
実行使用メモリ | 87,412 KB |
最終ジャッジ日時 | 2023-10-21 11:39:38 |
合計ジャッジ時間 | 15,274 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
53,376 KB |
testcase_01 | AC | 40 ms
53,376 KB |
testcase_02 | AC | 40 ms
53,376 KB |
testcase_03 | AC | 706 ms
82,328 KB |
testcase_04 | AC | 620 ms
82,260 KB |
testcase_05 | AC | 719 ms
82,608 KB |
testcase_06 | AC | 728 ms
82,240 KB |
testcase_07 | AC | 698 ms
82,708 KB |
testcase_08 | AC | 133 ms
77,404 KB |
testcase_09 | AC | 145 ms
77,540 KB |
testcase_10 | AC | 174 ms
77,608 KB |
testcase_11 | AC | 236 ms
78,360 KB |
testcase_12 | AC | 156 ms
77,644 KB |
testcase_13 | AC | 129 ms
77,140 KB |
testcase_14 | AC | 237 ms
78,712 KB |
testcase_15 | AC | 159 ms
77,952 KB |
testcase_16 | AC | 111 ms
76,380 KB |
testcase_17 | AC | 244 ms
78,036 KB |
testcase_18 | AC | 147 ms
77,432 KB |
testcase_19 | AC | 149 ms
77,428 KB |
testcase_20 | AC | 123 ms
77,040 KB |
testcase_21 | AC | 119 ms
76,456 KB |
testcase_22 | AC | 58 ms
68,336 KB |
testcase_23 | AC | 246 ms
77,868 KB |
testcase_24 | AC | 317 ms
79,652 KB |
testcase_25 | AC | 252 ms
78,420 KB |
testcase_26 | AC | 227 ms
78,364 KB |
testcase_27 | AC | 190 ms
77,828 KB |
testcase_28 | AC | 41 ms
53,376 KB |
testcase_29 | AC | 53 ms
63,840 KB |
testcase_30 | AC | 55 ms
66,200 KB |
testcase_31 | AC | 60 ms
68,328 KB |
testcase_32 | AC | 40 ms
53,376 KB |
testcase_33 | AC | 41 ms
53,376 KB |
testcase_34 | AC | 41 ms
53,376 KB |
testcase_35 | AC | 45 ms
59,280 KB |
testcase_36 | AC | 41 ms
53,376 KB |
testcase_37 | AC | 1,236 ms
87,404 KB |
testcase_38 | AC | 1,273 ms
87,412 KB |
testcase_39 | AC | 1,224 ms
87,296 KB |
testcase_40 | AC | 1,097 ms
86,968 KB |
testcase_41 | AC | 1,214 ms
87,164 KB |
testcase_42 | AC | 57 ms
70,332 KB |
testcase_43 | AC | 58 ms
70,332 KB |
testcase_44 | AC | 56 ms
70,332 KB |
ソースコード
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)