結果
問題 | No.1324 Approximate the Matrix |
ユーザー | shotoyoo |
提出日時 | 2020-12-21 12:24:16 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 6,174 bytes |
コンパイル時間 | 1,127 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 264,960 KB |
最終ジャッジ日時 | 2024-09-21 12:54:08 |
合計ジャッジ時間 | 4,703 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 63 ms
74,624 KB |
testcase_01 | AC | 62 ms
69,504 KB |
testcase_02 | AC | 63 ms
70,144 KB |
testcase_03 | TLE | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
ソースコード
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(2*10**5+10) write = lambda x: sys.stdout.write(x+"\n") from typing import NamedTuple, Optional, List, Tuple, cast from heapq import heappush, heappop class MCFGraph: class Edge(NamedTuple): src: int dst: int cap: int flow: int cost: int class _Edge: def __init__(self, dst: int, cap: int, cost: int) -> None: self.dst = dst self.cap = cap self.cost = cost self.rev: Optional[MCFGraph._Edge] = None def __init__(self, n: int, neg=False, negf=None) -> None: self._neg = neg if neg: n += 2 self._negs = n-2 self._negt = n-1 self._negf = negf self._negfsum = 0 self._negcsum = 0 self._negdone = False self._n = n self._g: List[List[MCFGraph._Edge]] = [[] for _ in range(n)] self._edges: List[MCFGraph._Edge] = [] def add_edge(self, src: int, dst: int, cap: int, cost: int) -> int: assert 0 <= src < self._n assert 0 <= dst < self._n assert 0 <= cap if cost<0 and self._neg: if not self._negdone: global s,t self._negdone = True self.add_edge(self._negs, s, self._negf, 0) self.add_edge(t, self._negt, self._negf, 0) # 後で指定した流量を流すための処理 # self._inf = 10**12 # self._negE = self.add_edge(self._negt, self._negs, self._negf, -self._inf) self.add_edge(self._negs, dst, cap, 0) self.add_edge(src, self._negt, cap, 0) self.add_edge(dst, src, cap, -cost) self._negfsum += cap self._negcsum += cap*cost else: m = len(self._edges) e = MCFGraph._Edge(dst, cap, cost) re = MCFGraph._Edge(src, 0, -cost) e.rev = re re.rev = e self._g[src].append(e) self._g[dst].append(re) self._edges.append(e) return m def get_edge(self, i: int) -> Edge: assert 0 <= i < len(self._edges) e = self._edges[i] re = cast(MCFGraph._Edge, e.rev) return MCFGraph.Edge( re.dst, e.dst, e.cap + re.cap, re.cap, e.cost ) def edges(self) -> List[Edge]: return [self.get_edge(i) for i in range(len(self._edges))] def flow(self, s: int, t: int, flow_limit: Optional[int] = None) -> Tuple[int, int]: if self._neg: flow_limit += self._negfsum val = self.slope(self._negs, self._negt, flow_limit)[-1] return (val[0], val[1] + self._negcsum) else: return self.slope(s, t, flow_limit)[-1] def slope(self, s: int, t: int, flow_limit: Optional[int] = None) -> List[Tuple[int, int]]: assert 0 <= s < self._n assert 0 <= t < self._n assert s != t if flow_limit is None: flow_limit = cast(int, sum(e.cap for e in self._g[s])) dual = [0] * self._n prev: List[Optional[Tuple[int, MCFGraph._Edge]]] = [None] * self._n def refine_dual() -> bool: pq = [(0, s)] visited = [False] * self._n dist: List[Optional[int]] = [None] * self._n dist[s] = 0 while pq: dist_v, v = heappop(pq) if visited[v]: continue visited[v] = True if v == t: break dual_v = dual[v] for e in self._g[v]: w = e.dst if visited[w] or e.cap == 0: continue reduced_cost = e.cost - dual[w] + dual_v new_dist = dist_v + reduced_cost dist_w = dist[w] if dist_w is None or new_dist < dist_w: dist[w] = new_dist prev[w] = v, e heappush(pq, (new_dist, w)) else: return False dist_t = dist[t] for v in range(self._n): if visited[v]: dual[v] -= cast(int, dist_t) - cast(int, dist[v]) return True flow = 0 cost = 0 prev_cost_per_flow: Optional[int] = None result = [(flow, cost)] while flow < flow_limit: if not refine_dual(): break f = flow_limit - flow v = t while prev[v] is not None: u, e = cast(Tuple[int, MCFGraph._Edge], prev[v]) f = min(f, e.cap) v = u v = t while prev[v] is not None: u, e = cast(Tuple[int, MCFGraph._Edge], prev[v]) e.cap -= f assert e.rev is not None e.rev.cap += f v = u c = -dual[s] flow += f cost += f * c if c == prev_cost_per_flow: result.pop() result.append((flow, cost)) prev_cost_per_flow = c return result n,k = list(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)] num = 2*n+n**2 + 2 g = MCFGraph(num, neg=True, negf=k) s = num-2 t = num-1 def pij(i,j): return i*n+j + 2*n for i in range(n): for j in range(n): g.add_edge(i,pij(i,j),min(a[i], b[j]),0) for ind in range(min(a[i],b[j])): cost = -2*p[i][j] + 1 + 2*ind g.add_edge(pij(i,j),n+j,1,cost) # print(i,j,cost) # g.add_edge(pij(i,j), t, a[i]+b[j], cost) # cost<0のときは? for i in range(n): g.add_edge(s, i, a[i], 0) for j in range(n): g.add_edge(n+j, t, b[j], 0) val = g.flow(s,t,k) ans = sum(p[i][j]**2 for i in range(n) for j in range(n)) + val[1] print(ans)