結果
問題 | No.177 制作進行の宮森あおいです! |
ユーザー | qib |
提出日時 | 2022-12-25 19:26:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 85 ms / 2,000 ms |
コード長 | 2,348 bytes |
コンパイル時間 | 171 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 72,960 KB |
最終ジャッジ日時 | 2024-04-30 00:06:29 |
合計ジャッジ時間 | 1,890 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
54,528 KB |
testcase_01 | AC | 49 ms
54,528 KB |
testcase_02 | AC | 48 ms
54,400 KB |
testcase_03 | AC | 54 ms
60,800 KB |
testcase_04 | AC | 53 ms
60,160 KB |
testcase_05 | AC | 54 ms
60,672 KB |
testcase_06 | AC | 62 ms
64,768 KB |
testcase_07 | AC | 58 ms
60,672 KB |
testcase_08 | AC | 52 ms
60,032 KB |
testcase_09 | AC | 81 ms
71,936 KB |
testcase_10 | AC | 80 ms
71,424 KB |
testcase_11 | AC | 74 ms
68,480 KB |
testcase_12 | AC | 85 ms
72,960 KB |
testcase_13 | AC | 46 ms
54,272 KB |
testcase_14 | AC | 46 ms
54,400 KB |
testcase_15 | AC | 45 ms
54,144 KB |
ソースコード
from collections import deque class Dinic: def __init__(self, n, flow_limit): self.vertices = n self.flow_limit = flow_limit self.links = [[] for _ in range(n)] self.level = None self.progress = None def add_link(self, f, t, cap): from_id = len(self.links[f]) to_id = len(self.links[t]) if f == t: to_id += 1 self.links[f].append([cap, t, to_id]) self.links[t].append([0, f, from_id]) def bfs(self, src, dst): level = [-1 for _ in range(self.vertices)] level[src] = 0 dq = deque([src]) while len(dq) > 0: cur = dq.popleft() for cap, nxt, _ in self.links[cur]: if cap > 0 and level[nxt] < 0: level[nxt] = level[cur] + 1 dq.append(nxt) self.level = level return level[dst] != -1 def dfs(self, cur, src, up): if cur == src: return up ans = 0 lv_cur = self.level[cur] for i in range(self.progress[cur], len(self.links[cur])): self.progress[cur] += 1 cap, nxt, rev = self.links[cur][i] if self.level[nxt] >= lv_cur or self.links[nxt][rev][0] == 0: continue d = self.dfs(nxt, src, min(up - ans, self.links[nxt][rev][0])) if d <= 0: continue self.links[cur][i][0] += d self.links[nxt][rev][0] -= d ans += d if ans == up: break return ans def max_flow(self, src, dst): ans = 0 while ans < self.flow_limit: if not self.bfs(src, dst): break self.progress = [-1 for _ in range(self.vertices)] while ans < self.flow_limit: f = self.dfs(dst, src, self.flow_limit - ans) if f <= 0: break ans += f return ans INF = 10 ** 13 w = int(input()) n = int(input()) j = list(map(int, input().split())) m = int(input()) c = list(map(int, input().split())) src = n + m dst = src + 1 mf = Dinic(n + m + 2, INF) banned = [[False for _ in range(n)] for _ in range(m)] for u in range(m): _, *xs = list(map(int, input().split())) for v in xs: banned[u][v - 1] = True for v in range(n): mf.add_link(src, v, j[v]) for u in range(m): mf.add_link(n + u, dst, c[u]) for u in range(m): for v in range(n): if not banned[u][v]: mf.add_link(v, n + u, INF) ans = mf.max_flow(src, dst) if ans < w: print("BANSAKUTSUKITA") else: print("SHIROBAKO")