Created
October 8, 2017 13:54
-
-
Save nariaki3551/60772a21c0bda4751db8b0be3231190b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# yukicoder No.572 妖精の演奏 | |
# https://yukicoder.me/problems/no/572 | |
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)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment