結果

問題 No.572 妖精の演奏
ユーザー compass19compass19
提出日時 2017-10-08 09:44:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 896 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 10,872 KB
実行使用メモリ 9,512 KB
最終ジャッジ日時 2023-08-10 20:06:30
合計ジャッジ時間 2,081 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,364 KB
testcase_01 AC 19 ms
8,372 KB
testcase_02 AC 16 ms
8,228 KB
testcase_03 AC 17 ms
8,240 KB
testcase_04 AC 18 ms
8,264 KB
testcase_05 AC 24 ms
8,252 KB
testcase_06 AC 27 ms
8,612 KB
testcase_07 AC 20 ms
8,496 KB
testcase_08 AC 22 ms
8,436 KB
testcase_09 AC 24 ms
8,368 KB
testcase_10 AC 82 ms
8,732 KB
testcase_11 AC 126 ms
9,000 KB
testcase_12 AC 146 ms
9,204 KB
testcase_13 AC 195 ms
9,512 KB
testcase_14 AC 44 ms
8,668 KB
testcase_15 AC 54 ms
8,520 KB
testcase_16 AC 42 ms
8,532 KB
testcase_17 AC 56 ms
8,612 KB
testcase_18 AC 21 ms
8,476 KB
testcase_19 AC 16 ms
8,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import product
n = int(input())-1
m = int(input())
M = list(range(m))
A = [list(map(int, input().split())) for _ in M]

# nを二進展開(n = sum([2**b for b in B]))
B = []
e = 0
while n != 0:
    if (n ^ 1<<e) < n:
        B.append(e)
        n ^= 1<<e
    e += 1

# DP[i][j][z] = iからjへ2**zで移動するときの最大利潤
DP = [[[0 for z in range(B[-1]+1)] for j in M] for i in M]
for z in range(B[-1]+1):
    for i, j in product(M, M):
        if z == 0:
            DP[i][j][z] = A[i][j]
        else:
            DP[i][j][z] = max([DP[i][k][z-1] + DP[k][j][z-1] for k in M])

# DQ[i][j] = iからjへ移動するときの最大利潤
for z, b in enumerate(B):
    if z == 0:
        DQ = [[DP[i][j][b] for j in M] for i in M]
    else:
        DQ = [[max([DQ[i][k] + DP[k][j][b] for k in M]) for j in M] for i in M]

print(max([DQ[i][j] for i, j in product(M, M)]))
0