結果

問題 No.572 妖精の演奏
ユーザー compass19compass19
提出日時 2017-10-08 09:44:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 306 ms / 2,000 ms
コード長 896 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2024-04-28 13:21:36
合計ジャッジ時間 2,588 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,880 KB
testcase_01 AC 32 ms
10,752 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 29 ms
10,752 KB
testcase_04 AC 30 ms
10,880 KB
testcase_05 AC 39 ms
11,008 KB
testcase_06 AC 45 ms
11,008 KB
testcase_07 AC 36 ms
10,880 KB
testcase_08 AC 39 ms
11,008 KB
testcase_09 AC 43 ms
11,008 KB
testcase_10 AC 126 ms
11,264 KB
testcase_11 AC 200 ms
11,648 KB
testcase_12 AC 230 ms
11,776 KB
testcase_13 AC 306 ms
12,032 KB
testcase_14 AC 77 ms
11,008 KB
testcase_15 AC 87 ms
11,264 KB
testcase_16 AC 72 ms
11,008 KB
testcase_17 AC 91 ms
11,264 KB
testcase_18 AC 38 ms
11,008 KB
testcase_19 AC 32 ms
10,880 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