結果
問題 | No.1324 Approximate the Matrix |
ユーザー | ああいい |
提出日時 | 2022-04-14 11:15:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 934 ms / 2,000 ms |
コード長 | 2,578 bytes |
コンパイル時間 | 187 ms |
コンパイル使用メモリ | 82,396 KB |
実行使用メモリ | 90,424 KB |
最終ジャッジ日時 | 2024-06-06 14:05:02 |
合計ジャッジ時間 | 13,086 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
54,920 KB |
testcase_01 | AC | 39 ms
54,380 KB |
testcase_02 | AC | 39 ms
54,004 KB |
testcase_03 | AC | 506 ms
83,968 KB |
testcase_04 | AC | 536 ms
83,924 KB |
testcase_05 | AC | 544 ms
84,440 KB |
testcase_06 | AC | 511 ms
83,968 KB |
testcase_07 | AC | 525 ms
84,012 KB |
testcase_08 | AC | 151 ms
77,944 KB |
testcase_09 | AC | 170 ms
78,596 KB |
testcase_10 | AC | 195 ms
78,992 KB |
testcase_11 | AC | 243 ms
80,236 KB |
testcase_12 | AC | 174 ms
78,444 KB |
testcase_13 | AC | 152 ms
78,032 KB |
testcase_14 | AC | 251 ms
80,460 KB |
testcase_15 | AC | 179 ms
78,784 KB |
testcase_16 | AC | 126 ms
77,284 KB |
testcase_17 | AC | 255 ms
80,372 KB |
testcase_18 | AC | 170 ms
78,332 KB |
testcase_19 | AC | 162 ms
77,888 KB |
testcase_20 | AC | 127 ms
77,864 KB |
testcase_21 | AC | 144 ms
77,436 KB |
testcase_22 | AC | 60 ms
70,160 KB |
testcase_23 | AC | 237 ms
79,424 KB |
testcase_24 | AC | 293 ms
81,432 KB |
testcase_25 | AC | 257 ms
80,252 KB |
testcase_26 | AC | 242 ms
80,184 KB |
testcase_27 | AC | 194 ms
78,812 KB |
testcase_28 | AC | 39 ms
53,064 KB |
testcase_29 | AC | 49 ms
63,644 KB |
testcase_30 | AC | 50 ms
63,128 KB |
testcase_31 | AC | 59 ms
67,348 KB |
testcase_32 | AC | 40 ms
54,604 KB |
testcase_33 | AC | 40 ms
53,840 KB |
testcase_34 | AC | 41 ms
53,620 KB |
testcase_35 | AC | 44 ms
59,328 KB |
testcase_36 | AC | 41 ms
53,724 KB |
testcase_37 | AC | 934 ms
90,424 KB |
testcase_38 | AC | 869 ms
89,852 KB |
testcase_39 | AC | 826 ms
89,876 KB |
testcase_40 | AC | 895 ms
89,748 KB |
testcase_41 | AC | 842 ms
89,752 KB |
testcase_42 | AC | 58 ms
70,848 KB |
testcase_43 | AC | 57 ms
70,624 KB |
testcase_44 | AC | 57 ms
71,700 KB |
ソースコード
#その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 #if v == t:break 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 if d < inf else h 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)