結果
問題 | No.1678 Coin Trade (Multiple) |
ユーザー | chocorusk |
提出日時 | 2021-08-02 03:59:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 2,450 ms / 5,000 ms |
コード長 | 5,534 bytes |
コンパイル時間 | 289 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 268,940 KB |
最終ジャッジ日時 | 2024-06-11 21:35:41 |
合計ジャッジ時間 | 39,819 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
53,976 KB |
testcase_01 | AC | 37 ms
53,760 KB |
testcase_02 | AC | 38 ms
54,016 KB |
testcase_03 | AC | 470 ms
123,724 KB |
testcase_04 | AC | 1,439 ms
210,916 KB |
testcase_05 | AC | 712 ms
155,712 KB |
testcase_06 | AC | 589 ms
146,056 KB |
testcase_07 | AC | 1,391 ms
201,288 KB |
testcase_08 | AC | 895 ms
167,560 KB |
testcase_09 | AC | 712 ms
160,604 KB |
testcase_10 | AC | 361 ms
102,136 KB |
testcase_11 | AC | 905 ms
180,980 KB |
testcase_12 | AC | 355 ms
110,388 KB |
testcase_13 | AC | 1,936 ms
264,732 KB |
testcase_14 | AC | 857 ms
156,608 KB |
testcase_15 | AC | 765 ms
162,368 KB |
testcase_16 | AC | 375 ms
127,024 KB |
testcase_17 | AC | 765 ms
161,180 KB |
testcase_18 | AC | 52 ms
65,408 KB |
testcase_19 | AC | 37 ms
54,272 KB |
testcase_20 | AC | 43 ms
61,952 KB |
testcase_21 | AC | 47 ms
62,848 KB |
testcase_22 | AC | 52 ms
65,152 KB |
testcase_23 | AC | 42 ms
61,184 KB |
testcase_24 | AC | 52 ms
64,896 KB |
testcase_25 | AC | 40 ms
54,400 KB |
testcase_26 | AC | 46 ms
63,360 KB |
testcase_27 | AC | 43 ms
61,440 KB |
testcase_28 | AC | 42 ms
61,056 KB |
testcase_29 | AC | 39 ms
54,528 KB |
testcase_30 | AC | 43 ms
61,552 KB |
testcase_31 | AC | 38 ms
54,784 KB |
testcase_32 | AC | 37 ms
53,760 KB |
testcase_33 | AC | 49 ms
65,280 KB |
testcase_34 | AC | 49 ms
64,000 KB |
testcase_35 | AC | 42 ms
54,912 KB |
testcase_36 | AC | 52 ms
64,896 KB |
testcase_37 | AC | 52 ms
65,664 KB |
testcase_38 | AC | 43 ms
61,568 KB |
testcase_39 | AC | 43 ms
60,416 KB |
testcase_40 | AC | 50 ms
64,256 KB |
testcase_41 | AC | 48 ms
61,952 KB |
testcase_42 | AC | 48 ms
63,488 KB |
testcase_43 | AC | 54 ms
66,176 KB |
testcase_44 | AC | 38 ms
54,400 KB |
testcase_45 | AC | 38 ms
54,400 KB |
testcase_46 | AC | 54 ms
66,944 KB |
testcase_47 | AC | 47 ms
62,976 KB |
testcase_48 | AC | 2,250 ms
266,424 KB |
testcase_49 | AC | 2,380 ms
266,472 KB |
testcase_50 | AC | 2,283 ms
266,964 KB |
testcase_51 | AC | 2,388 ms
267,012 KB |
testcase_52 | AC | 2,450 ms
267,540 KB |
testcase_53 | AC | 2,223 ms
268,568 KB |
testcase_54 | AC | 2,249 ms
267,024 KB |
testcase_55 | AC | 2,318 ms
267,764 KB |
testcase_56 | AC | 2,315 ms
268,940 KB |
testcase_57 | AC | 2,228 ms
266,560 KB |
testcase_58 | AC | 874 ms
168,796 KB |
ソースコード
import sys read=sys.stdin.buffer.read readline=sys.stdin.buffer.readline readlines=sys.stdin.buffer.readlines # https://atcoder.jp/contests/practice2/submissions/20652483 import heapq class mcf_graph: def __init__(self, n): self.n = n self._edges = [] def add_edge(self, from_, to, cap, cost): assert 0 <= from_ < self.n assert 0 <= to < self.n assert 0 <= cap assert 0 <= cost m = len(self._edges) self._edges.append(self.__class__.edge(from_, to, cap, 0, cost)) return m class edge: def __init__(self, from_, to, cap, flow, cost): self.from_ = from_ self.to = to self.cap = cap self.flow = flow self.cost = cost def get_edge(self, i): m = len(self._edges) assert 0 <= i < m return self._edges[i] def edges(self): return self._edges.copy() def _dual_ref(self, s, t): self.dist = [float('inf')] * self.n self.vis = [False] * self.n que_min = [] que = [] que_push_que = [] self.dist[s] = 0 que_min.append(s) while que_min or que or que_push_que: if que_min: v = que_min.pop() else: for e in que_push_que: heapq.heappush(que, e) que_push_que.clear() _, v = heapq.heappop(que) if self.vis[v]: continue self.vis[v] = True if v == t: break dual_v = self.dual[v] dist_v = self.dist[v] for i in range(self.start[v], self.start[v + 1]): e = self.elist[i] if not e.cap: continue cost = e.cost - self.dual[e.to] + dual_v if self.dist[e.to] - dist_v > cost: dist_to = dist_v + cost self.dist[e.to] = dist_to self.prev_e[e.to] = e.rev if dist_to == dist_v: que_min.append(e.to) else: que_push_que.append((dist_to, e.to)) if not self.vis[t]: return False for v in range(self.n): if not self.vis[v]: continue self.dual[v] -= self.dist[t] - self.dist[v] return True def _csr(self): m = len(self._edges) self.edge_idx = [0] * m redge_idx = [0] * m degree = [0] * self.n edges = [] for i, e in enumerate(self._edges): self.edge_idx[i] = degree[e.from_] degree[e.from_] += 1 redge_idx[i] = degree[e.to] degree[e.to] += 1 edges.append((e.from_, self.__class__._edge(e.to, -1, e.cap - e.flow, e.cost))) edges.append((e.to, self.__class__._edge(e.from_, -1, e.flow, -e.cost))) self.start = [0] * (self.n + 1) self.elist = [0] * len(edges) for v, w in edges: self.start[v + 1] += 1 for i in range(1, self.n + 1): self.start[i] += self.start[i-1] counter = self.start.copy() for v, w in edges: self.elist[counter[v]] = w counter[v] += 1 for i, e in enumerate(self._edges): self.edge_idx[i] += self.start[e.from_] redge_idx[i] += self.start[e.to] self.elist[self.edge_idx[i]].rev = redge_idx[i] self.elist[redge_idx[i]].rev = self.edge_idx[i] def slope(self, s, t, flow_limit=float('inf')): assert 0 <= s < self.n assert 0 <= t < self.n assert s != t self._csr() self.dual = [0] * self.n self.dist = [float('inf')] * self.n self.prev_e = [0] * self.n self.vis = [False] * self.n flow = 0 cost = 0 prev_cost_per_flow = -1 result = [(0, 0)] while flow < flow_limit: if not self._dual_ref(s, t): break c = flow_limit - flow v = t while v != s: c = min(c, self.elist[self.elist[self.prev_e[v]].rev].cap) v = self.elist[self.prev_e[v]].to v = t while v != s: e = self.elist[self.prev_e[v]] e.cap += c self.elist[e.rev].cap -= c v = self.elist[self.prev_e[v]].to d = -self.dual[s] flow += c cost += c * d if prev_cost_per_flow == d: result.pop() result.append((flow, cost)) prev_cost_per_flow = d for i in range(len(self._edges)): e = self.elist[self.edge_idx[i]] self._edges[i].flow = self._edges[i].cap - e.cap return result def flow(self, s, t, flow_limit=float('inf')): return self.slope(s, t, flow_limit)[-1] class _edge: def __init__(self, to, rev, cap, cost): self.to = to self.rev = rev self.cap = cap self.cost = cost n, k=map(int, readline().split()) inf=10**9 g=mcf_graph(n) a=[0]*n for i in range(n): a[i], m=map(int, readline().split()) b=list(map(int, readline().split())) for bi in b: bi-=1 g.add_edge(bi, i, 1, a[bi]-a[i]+inf*(i-bi)) for i in range(n-1): g.add_edge(i, i+1, k, inf) print(inf*(n-1)*k-g.flow(0, n-1, k)[1])