結果

問題 No.1780 [Cherry Anniversary] 真冬に咲く26の櫻の木
ユーザー 👑 KazunKazun
提出日時 2021-11-22 20:12:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 372 ms / 2,000 ms
コード長 1,803 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 78,600 KB
最終ジャッジ日時 2023-09-23 11:28:36
合計ジャッジ時間 11,814 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
76,540 KB
testcase_01 AC 100 ms
77,196 KB
testcase_02 AC 122 ms
77,508 KB
testcase_03 AC 128 ms
77,124 KB
testcase_04 AC 136 ms
77,680 KB
testcase_05 AC 125 ms
77,612 KB
testcase_06 AC 129 ms
77,628 KB
testcase_07 AC 129 ms
77,600 KB
testcase_08 AC 138 ms
77,852 KB
testcase_09 AC 131 ms
77,592 KB
testcase_10 AC 137 ms
77,320 KB
testcase_11 AC 140 ms
77,636 KB
testcase_12 AC 128 ms
77,708 KB
testcase_13 AC 120 ms
77,292 KB
testcase_14 AC 263 ms
77,992 KB
testcase_15 AC 359 ms
78,428 KB
testcase_16 AC 323 ms
78,436 KB
testcase_17 AC 251 ms
78,136 KB
testcase_18 AC 311 ms
77,968 KB
testcase_19 AC 237 ms
78,600 KB
testcase_20 AC 303 ms
78,028 KB
testcase_21 AC 360 ms
78,424 KB
testcase_22 AC 368 ms
78,432 KB
testcase_23 AC 265 ms
78,324 KB
testcase_24 AC 372 ms
78,076 KB
testcase_25 AC 262 ms
78,380 KB
testcase_26 AC 244 ms
78,272 KB
testcase_27 AC 298 ms
78,396 KB
testcase_28 AC 237 ms
78,244 KB
testcase_29 AC 227 ms
77,944 KB
testcase_30 AC 360 ms
78,100 KB
testcase_31 AC 217 ms
78,472 KB
testcase_32 AC 220 ms
78,148 KB
testcase_33 AC 288 ms
78,284 KB
testcase_34 AC 82 ms
75,948 KB
testcase_35 AC 165 ms
77,376 KB
testcase_36 AC 82 ms
75,624 KB
testcase_37 AC 164 ms
77,372 KB
testcase_38 AC 205 ms
77,812 KB
testcase_39 AC 190 ms
77,464 KB
testcase_40 AC 200 ms
77,956 KB
testcase_41 AC 204 ms
77,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#想定解法

def member_id(alpha):
    return ord(alpha)-65 # ord("A")=65

def identity_matrix(size):
    return [[0 if i==j else -inf for j in range(size)] for i in range(size)]

def tolopical_matrix_add(A,B):
    C=[[0]*number for _ in range(number)]
    for i in range(number):
        a=A[i]; b=B[i]; c=C[i]
        for j in range(number):
            c[j]=max(a[j],b[j])
    return C

def tolopical_matrix_mul(A,B):
    C=identity_matrix(number)
    for i in range(number):
        ci=C[i]; ai=A[i]
        for k in range(number):
            aik=ai[k]; bk=B[k]
            for j in range(number):
                ci[j]=max(ci[j],aik+bk[j])
    return C

def tolopical_matrix_power(A,k):
    B=identity_matrix(number)
    while True:
        if k&1:
            B=tolopical_matrix_mul(B,A)

        k>>=1
        if k:
            A=tolopical_matrix_mul(A,A)
        else:
            break
    return B

#==================================================
import sys
input=sys.stdin.readline

#==================================================
# #入力部
C=list(map(int,input().split()))
K=list(map(int,input().split()))

inf=float("inf"); number=16; member=26
M=[identity_matrix(number) for _ in range(member)]
N=int(input())

for _ in range(N):
    S,A,B,E=input().split()
    A=int(A)-1; B=int(B)-1; E=int(E)

    for s in S:
        M[member_id(s)][A][B]=M[member_id(s)][B][A]=max(M[member_id(s)][A][B],E)

#==================================================
#トロピカル代数上での累乗を求める.
T=[tolopical_matrix_power(M[alpha],K[alpha])[C[alpha]-1] for alpha in range(member)]

#==================================================
#最終解答

V=[0]*number
for t in T:
    for i in range(number):
        V[i]+=t[i]

ans=max(V)
print(ans if ans>-inf else "Impossible")
0