結果
問題 | No.2713 Just Solitaire |
ユーザー | 👑 Nafmo2 |
提出日時 | 2024-03-12 02:23:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 64 ms / 2,000 ms |
コード長 | 3,981 bytes |
コンパイル時間 | 155 ms |
コンパイル使用メモリ | 82,140 KB |
実行使用メモリ | 74,784 KB |
最終ジャッジ日時 | 2024-09-29 22:08:43 |
合計ジャッジ時間 | 2,878 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
55,372 KB |
testcase_01 | AC | 36 ms
55,408 KB |
testcase_02 | AC | 38 ms
56,320 KB |
testcase_03 | AC | 40 ms
57,044 KB |
testcase_04 | AC | 51 ms
68,656 KB |
testcase_05 | AC | 40 ms
57,268 KB |
testcase_06 | AC | 62 ms
73,688 KB |
testcase_07 | AC | 43 ms
62,428 KB |
testcase_08 | AC | 38 ms
55,188 KB |
testcase_09 | AC | 38 ms
56,816 KB |
testcase_10 | AC | 39 ms
56,564 KB |
testcase_11 | AC | 52 ms
68,024 KB |
testcase_12 | AC | 39 ms
55,956 KB |
testcase_13 | AC | 55 ms
68,324 KB |
testcase_14 | AC | 51 ms
67,296 KB |
testcase_15 | AC | 38 ms
55,456 KB |
testcase_16 | AC | 38 ms
55,468 KB |
testcase_17 | AC | 50 ms
67,356 KB |
testcase_18 | AC | 39 ms
56,228 KB |
testcase_19 | AC | 54 ms
69,692 KB |
testcase_20 | AC | 37 ms
55,224 KB |
testcase_21 | AC | 38 ms
56,292 KB |
testcase_22 | AC | 38 ms
56,236 KB |
testcase_23 | AC | 51 ms
67,424 KB |
testcase_24 | AC | 54 ms
69,500 KB |
testcase_25 | AC | 61 ms
73,660 KB |
testcase_26 | AC | 53 ms
68,912 KB |
testcase_27 | AC | 60 ms
72,532 KB |
testcase_28 | AC | 59 ms
73,212 KB |
testcase_29 | AC | 64 ms
74,784 KB |
testcase_30 | AC | 62 ms
72,288 KB |
testcase_31 | AC | 54 ms
69,452 KB |
testcase_32 | AC | 64 ms
74,128 KB |
testcase_33 | AC | 63 ms
73,956 KB |
ソースコード
#! /usr/bin/env pypy3 from collections import deque class mf_graph: n = 1 g = [[] for i in range(1)] pos = [] def __init__(self, N): self.n = N self.g = [[] for i in range(N)] self.pos = [] def add_edge(self, From, To, cap): assert 0 <= From and From < self.n assert 0 <= To and To < self.n assert 0 <= cap m = len(self.pos) from_id = len(self.g[From]) self.pos.append([From, from_id]) to_id = len(self.g[To]) if From == To: to_id += 1 self.g[From].append([To, to_id, cap]) self.g[To].append([From, from_id, 0]) return m def get_edge(self, i): m = len(self.pos) assert 0 <= i and i < m _e = self.g[self.pos[i][0]][self.pos[i][1]] _re = self.g[_e[0]][_e[1]] return [self.pos[i][0], _e[0], _e[2] + _re[2], _re[2]] def edges(self): m = len(self.pos) result = [] for i in range(m): a, b, c, d = self.get_edge(i) result.append({"from": a, "to": b, "cap": c, "flow": d}) return result def change_edge(self, i, new_cap, new_flow): m = len(self.pos) assert 0 <= i and i < m assert 0 <= new_flow and new_flow <= new_cap _e = self.g[self.pos[i][0]][self.pos[i][1]] _re = self.g[_e[0]][_e[1]] _e[2] = new_cap - new_flow _re[2] = new_flow def flow(self, s, t, flow_limit=(1 << 63) - 1): assert 0 <= s and s < self.n assert 0 <= t and t < self.n assert s != t def bfs(): level = [-1 for i in range(self.n)] level[s] = 0 que = deque([]) que.append(s) while que: v = que.popleft() for to, rev, cap in self.g[v]: if cap == 0 or level[to] >= 0: continue level[to] = level[v] + 1 if to == t: return level que.append(to) return level def dfs(v, up): if v == s: return up res = 0 level_v = level[v] for i in range(Iter[v], len(self.g[v])): Iter[v] = i to, rev, cap = self.g[v][i] if level_v <= level[to] or self.g[to][rev][2] == 0: continue d = dfs(to, min(up - res, self.g[to][rev][2])) if d <= 0: continue self.g[v][i][2] += d self.g[to][rev][2] -= d res += d if res == up: return res level[v] = self.n return res flow = 0 while flow < flow_limit: level = bfs() if level[t] == -1: break Iter = [0 for i in range(self.n)] f = dfs(t, flow_limit - flow) if not (f): break flow += f return flow def min_cut(self, s): visited = [False for i in range(self.n)] que = deque([]) que.append(s) while len(que) > 0: p = que.popleft() visited[p] = True for to, rev, cap in self.g[p]: if cap and not (visited[to]): visited[to] = True que.append(to) return visited ans = 0 INF = 10**15 N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) s = N + M t = N + M + 1 graph = mf_graph(N + M + 2) for i in range(N): graph.add_edge(s, i, A[i]) for i in range(M): graph.add_edge(N + i, t, B[i]) for i in range(M): raw_C = list(map(int, input().split())) K = raw_C[0] C = raw_C[1:] for j in range(K): graph.add_edge(C[j] - 1, N + i, INF) ans = sum(B) - graph.flow(s, t) print(ans)