結果

問題 No.845 最長の切符
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-07-07 17:15:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,492 ms / 3,000 ms
コード長 595 bytes
コンパイル時間 146 ms
コンパイル使用メモリ 82,332 KB
実行使用メモリ 199,344 KB
最終ジャッジ日時 2024-04-15 07:54:14
合計ジャッジ時間 9,479 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,256 KB
testcase_01 AC 46 ms
54,956 KB
testcase_02 AC 46 ms
54,672 KB
testcase_03 AC 42 ms
54,700 KB
testcase_04 AC 42 ms
54,320 KB
testcase_05 AC 41 ms
55,680 KB
testcase_06 AC 42 ms
55,220 KB
testcase_07 AC 41 ms
54,904 KB
testcase_08 AC 62 ms
67,260 KB
testcase_09 AC 52 ms
61,212 KB
testcase_10 AC 98 ms
78,356 KB
testcase_11 AC 67 ms
70,124 KB
testcase_12 AC 84 ms
76,732 KB
testcase_13 AC 62 ms
67,548 KB
testcase_14 AC 62 ms
67,304 KB
testcase_15 AC 306 ms
93,144 KB
testcase_16 AC 1,462 ms
197,800 KB
testcase_17 AC 331 ms
92,384 KB
testcase_18 AC 671 ms
129,244 KB
testcase_19 AC 177 ms
84,864 KB
testcase_20 AC 1,382 ms
199,344 KB
testcase_21 AC 1,071 ms
194,520 KB
testcase_22 AC 326 ms
92,460 KB
testcase_23 AC 127 ms
80,256 KB
testcase_24 AC 1,492 ms
197,748 KB
testcase_25 AC 42 ms
55,112 KB
testcase_26 AC 41 ms
53,852 KB
testcase_27 AC 43 ms
53,496 KB
testcase_28 AC 56 ms
64,980 KB
testcase_29 AC 43 ms
54,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections

n, m = map(int, input().split())
d = [[-1] * n for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    d[a][b] = d[b][a] = max(d[a][b], c)

cache = {}
def f(current, history):
    ky = (current, history)
    if ky in cache:
        return cache[ky]
    
    ans = 0
    for nxt in range(n):
        if history & (1 << nxt) == 0 and d[current][nxt] > 0:
            ans = max(ans, f(nxt, history | (1 << current)) + d[current][nxt])
    cache[ky] = ans
    return ans

ans = max(f(current, 0) for current in range(n))
print(ans)
0