結果

問題 No.943 取り調べ
ユーザー ganariya
提出日時 2019-11-14 22:15:38
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,000 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 82,572 KB
実行使用メモリ 84,592 KB
最終ジャッジ日時 2024-09-24 22:58:48
合計ジャッジ時間 10,664 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
from copy import deepcopy

N = int(input())
X = [list(map(int, input().split())) for i in range(N)]
A = [int(x) for x in input().split()]

INK = [0] * N
for i in range(N):
    for j in range(N):
        if X[i][j]:
            INK[i] += 1


def isGood(bit):
    ink = deepcopy(INK)
    expanded = [False] * N

    Q = deque()
    for i in range(N):
        if bit & (1 << i):
            ink[i] = 0
        if ink[i] == 0:
            Q.append(i)

    while Q:
        d = Q.popleft()
        if expanded[d]:
            continue
        expanded[d] = True

        for i in range(N):
            if X[i][d] and (not expanded[i]):
                ink[i] -= 1
                if ink[i] == 0 and not expanded[i]:
                    Q.append(i)

    return expanded.count(True) == N


ans = 1e20

for bit in range(1 << N):
    cost = 0
    for i in range(N):
        if bit & (1 << i):
            cost += A[i]
    if isGood(bit):
        ans = min(ans, cost)

print(ans)
0