結果
問題 | No.1324 Approximate the Matrix |
ユーザー | ああいい |
提出日時 | 2022-04-14 11:02:40 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,760 bytes |
コンパイル時間 | 186 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 90,376 KB |
最終ジャッジ日時 | 2024-12-24 05:59:19 |
合計ジャッジ時間 | 15,622 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 TLE * 1 |
ソースコード
#その2 最初はコストが全部正の場合に使えるダイクストラ from heapq import heappush,heappop class MinCostFlow: inf = 10 ** 10 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 cost = cost - H[w] + H[v] if cap > 0 and dist[v] + cost < dist[w]: dist[w] = dist[v] + cost prev_v[w] = v prev_e[w] = e heappush(q,(dist[w],w)) if dist[t] == inf: return None """ for i in range(N): H[i] += dist[i] """ for i in range(N): if dist[i] < inf: H[i] += dist[i] - dist[t] d = f v = t while v != s: d = min(d,prev_e[v][1]) v = prev_v[v] f -= d res += d * -H[s] v = t while v != s: e = prev_e[v] e[1] -= d e[3][1] += d v = prev_v[v] return res import sys rr = sys.stdin N,K = map(int,rr.readline().split()) A = list(map(int,rr.readline().split())) B = list(map(int,rr.readline().split())) P = [list(map(int,rr.readline().split())) for _ in range(N)] mincost = MinCostFlow(N + N + 2) s = N + N t = N + N + 1 inf = 400 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,inf + (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) - inf * K print(ans)