結果

問題 No.177 制作進行の宮森あおいです!
ユーザー nehan_der_thalnehan_der_thal
提出日時 2020-03-06 17:57:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 2,195 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 82,916 KB
実行使用メモリ 70,912 KB
最終ジャッジ日時 2024-04-22 04:02:33
合計ジャッジ時間 1,782 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,016 KB
testcase_01 AC 43 ms
54,656 KB
testcase_02 AC 42 ms
53,888 KB
testcase_03 AC 42 ms
54,784 KB
testcase_04 AC 43 ms
54,912 KB
testcase_05 AC 50 ms
60,672 KB
testcase_06 AC 56 ms
65,408 KB
testcase_07 AC 48 ms
55,040 KB
testcase_08 AC 50 ms
61,056 KB
testcase_09 AC 71 ms
70,912 KB
testcase_10 AC 72 ms
69,504 KB
testcase_11 AC 73 ms
68,736 KB
testcase_12 AC 65 ms
68,224 KB
testcase_13 AC 46 ms
53,888 KB
testcase_14 AC 45 ms
54,016 KB
testcase_15 AC 50 ms
54,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
 
 
class Dinic:
    def __init__(self, n):
        self.n = n
        self.links = [[] for _ in range(n)]
        self.depth = None
        self.progress = None
 
    def add_link(self, _from, to, cap):
        self.links[_from].append([cap, to, len(self.links[to])])
        self.links[to].append([0, _from, len(self.links[_from]) - 1])
 
    def bfs(self, s):
        depth = [-1] * self.n
        depth[s] = 0
        q = deque([s])
        while q:
            v = q.popleft()
            for cap, to, rev in self.links[v]:
                if cap > 0 and depth[to] < 0:
                    depth[to] = depth[v] + 1
                    q.append(to)
        self.depth = depth
 
    def dfs(self, v, t, flow):
        if v == t:
            return flow
        links_v = self.links[v]
        for i in range(self.progress[v], len(links_v)):
            self.progress[v] = i
            cap, to, rev = link = links_v[i]
            if cap == 0 or self.depth[v] >= self.depth[to]:
                continue
            d = self.dfs(to, t, min(flow, cap))
            if d == 0:
                continue
            link[0] -= d
            self.links[to][rev][0] += d
            return d
        return 0
 
    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.depth[t] < 0:
                return flow
            self.progress = [0] * self.n
            current_flow = self.dfs(s, t, float('inf'))
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, float('inf'))
W, = map(int, input().split()) 
N, = map(int, input().split()) 
J = list(map(int, input().split()))
M, = map(int, input().split()) 
C = list(map(int, input().split()))
Xs = []
for _ in range(M):
    Q, *X = map(int, input().split()) 
    Xs.append(set(X))

inf = 10**18
mf = Dinic(N+M+2)
for i in range(N):
    mf.add_link(0, i+1, J[i])
    for j in range(M):
        if i+1 in Xs[j]: continue
        mf.add_link(i+1, N+j+1, inf)
for i in range(M):
    mf.add_link(N+i+1, N+M+1, C[i])

if mf.max_flow(0, N+M+1) < W:
    print("BANSAKUTSUKITA")
else:
    print("SHIROBAKO")
0