結果

問題 No.177 制作進行の宮森あおいです!
ユーザー terasaterasa
提出日時 2022-12-17 11:09:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 2,543 bytes
コンパイル時間 2,194 ms
コンパイル使用メモリ 82,084 KB
実行使用メモリ 79,104 KB
最終ジャッジ日時 2024-11-16 19:50:19
合計ジャッジ時間 2,255 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
68,096 KB
testcase_01 AC 66 ms
68,480 KB
testcase_02 AC 66 ms
68,352 KB
testcase_03 AC 75 ms
71,680 KB
testcase_04 AC 69 ms
69,248 KB
testcase_05 AC 78 ms
73,344 KB
testcase_06 AC 94 ms
78,464 KB
testcase_07 AC 68 ms
68,992 KB
testcase_08 AC 78 ms
73,932 KB
testcase_09 AC 99 ms
78,720 KB
testcase_10 AC 102 ms
79,104 KB
testcase_11 AC 106 ms
78,592 KB
testcase_12 AC 97 ms
78,336 KB
testcase_13 AC 64 ms
68,224 KB
testcase_14 AC 66 ms
68,480 KB
testcase_15 AC 73 ms
68,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List, Tuple, Callable, TypeVar, Optional
import sys
import itertools
import heapq
import bisect
import math
from collections import deque, defaultdict
from functools import lru_cache, cmp_to_key

input = sys.stdin.readline

if __file__ != 'prog.py':
    sys.setrecursionlimit(10 ** 6)


def readints(): return map(int, input().split())
def readlist(): return list(readints())
def readstr(): return input()[:-1]


class Dinic:
    def __init__(self, N, inf=1 << 50):
        self.N = N
        self.G = [[] for _ in range(self.N)]
        self.inf = inf

    def add_edge(self, u, v, cap):
        forward = [cap, v, None]
        backward = [0, u, None]
        forward[2] = backward
        backward[2] = forward
        self.G[u].append(forward)
        self.G[v].append(backward)

    def flow(self, s, t):
        flow = 0
        while True:
            self._bfs(s, t)
            if self.level[t] is None:
                return flow
            *self.iter, = map(iter, self.G)
            f = self.inf
            while f > 0:
                f = self._dfs(s, t, self.inf)
                flow += f

    def _bfs(self, s, t):
        self.level = [None] * self.N
        dq = deque([s])
        self.level[s] = 0
        while dq:
            v = dq.popleft()
            for cap, d, _ in self.G[v]:
                if cap == 0:
                    continue
                if self.level[d] is None:
                    self.level[d] = self.level[v] + 1
                    dq.append(d)

    def _dfs(self, u, t, f):
        if u == t:
            return f
        for e in self.iter[u]:
            cap, v, rev = e
            if cap == 0:
                continue
            if self.level[v] > self.level[u]:
                delta = self._dfs(v, t, min(f, cap))
                if delta > 0:
                    e[0] -= delta
                    rev[0] += delta
                    return delta
        return 0


W = int(input())
N = int(input())
J = readlist()
M = int(input())
C = readlist()
comb = [[True for _ in range(M)] for _ in range(N)]
for j in range(M):
    q, *X = readints()
    for i in X:
        i -= 1
        comb[i][j] = False

solver = Dinic(N + M + 2)
INF = 1 << 50
for i in range(N):
    solver.add_edge(0, i + 1, J[i])
for j in range(M):
    solver.add_edge(N + 1 + j, N + 1 + M, C[j])
for i in range(N):
    for j in range(M):
        if comb[i][j]:
            solver.add_edge(i + 1, N + 1 + j, INF)
flow = solver.flow(0, N + M + 1)
print('SHIROBAKO' if flow >= W else 'BANSAKUTSUKITA')
0