結果
問題 | No.177 制作進行の宮森あおいです! |
ユーザー | maspy |
提出日時 | 2020-03-24 22:32:14 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 30 ms / 2,000 ms |
コード長 | 2,946 bytes |
コンパイル時間 | 185 ms |
コンパイル使用メモリ | 13,056 KB |
実行使用メモリ | 11,392 KB |
最終ジャッジ日時 | 2024-06-10 09:25:46 |
合計ジャッジ時間 | 1,400 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 27 ms
11,008 KB |
testcase_01 | AC | 26 ms
11,136 KB |
testcase_02 | AC | 25 ms
10,880 KB |
testcase_03 | AC | 27 ms
10,880 KB |
testcase_04 | AC | 27 ms
11,008 KB |
testcase_05 | AC | 27 ms
11,264 KB |
testcase_06 | AC | 28 ms
11,136 KB |
testcase_07 | AC | 26 ms
11,264 KB |
testcase_08 | AC | 26 ms
10,880 KB |
testcase_09 | AC | 29 ms
11,392 KB |
testcase_10 | AC | 28 ms
11,264 KB |
testcase_11 | AC | 30 ms
11,392 KB |
testcase_12 | AC | 29 ms
11,392 KB |
testcase_13 | AC | 26 ms
11,008 KB |
testcase_14 | AC | 27 ms
11,136 KB |
testcase_15 | AC | 27 ms
10,880 KB |
ソースコード
#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from collections import deque class Dinic: def __init__(self, N, source, sink): self.N = N self.G = [[] for _ in range(N)] self.source = source self.sink = sink def add_edge(self, fr, to, cap): n1 = len(self.G[fr]) n2 = len(self.G[to]) self.G[fr].append([to, cap, n2]) self.G[to].append([fr, 0, n1]) # 逆辺を cap 0 で追加 def add_edge_undirected(self, fr, to, cap): n1 = len(self.G[fr]) n2 = len(self.G[to]) self.G[fr].append([to, cap, n2]) self.G[to].append([fr, cap, n1]) def bfs(self): level = [0] * self.N G = self.G; source = self.source; sink = self.sink q = deque([source]) level[source] = 1 pop = q.popleft; append = q.append while q: v = pop() lv = level[v] + 1 for to, cap, rev in G[v]: if not cap: continue if level[to]: continue level[to] = lv if to == sink: self.level = level return append(to) self.level = level def dfs(self, v, f): if v == self.sink: return f G = self.G prog = self.progress level = self.level lv = level[v] E = G[v] for i in range(prog[v], len(E)): to, cap, rev = E[i] prog[v] = i if not cap: continue if level[to] <= lv: continue x = f if f < cap else cap ff = self.dfs(to, x) if ff: E[i][1] -= ff G[to][rev][1] += ff return ff return 0 def max_flow(self): INF = 10**18 flow = 0 while True: self.bfs() if not self.level[self.sink]: return flow self.progress = [0] * self.N while True: f = self.dfs(self.source, INF) if not f: break flow += f return flow W = int(readline()) N = int(readline()) J = tuple(map(int, readline().split())) M = int(readline()) C = tuple(map(int, readline().split())) source = 0 sink = N + M + 1 dinic = Dinic(N + M + 2, source, sink) add = dinic.add_edge for i, x in enumerate(J, 1): add(source, i, x) for i, x in enumerate(C, 1): add(N + i, sink, x) INF = W + 10 for i, line in enumerate(readlines(), 1): Q, *X = map(int, line.split()) X = set(X) for j in range(1, N + 1): if j in X: continue add(j, N + i, INF) f = dinic.max_flow() answer = 'SHIROBAKO' if f >= W else 'BANSAKUTSUKITA' print(answer)