結果

問題 No.357 品物の並び替え (Middle)
ユーザー lloyzlloyz
提出日時 2023-10-30 00:12:02
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 707 bytes
コンパイル時間 405 ms
コンパイル使用メモリ 81,840 KB
実行使用メモリ 79,208 KB
最終ジャッジ日時 2023-10-30 00:12:05
合計ジャッジ時間 3,081 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

n, m = map(int, input().split())
G = defaultdict(list)
for _ in range(m):
    a, b, p = map(int, input().split())
    G[a].append((b, p))
DP = [[-1 for _ in range(n)] for _ in range(1 << n)]
for i in range(n):
    DP[1 << i][i] = 0
for bit in range(1 << n):
    for i in range(n):
        if (bit >> i) & 1:
            if DP[bit][i] != -1:
                for j, p in G[i]:
                    if (bit >> j) & 1 == 0:
                        nbit = bit | (1 << j)
                        DP[nbit][j] = max(DP[nbit][j], DP[bit][i] + p)
ans = 0
for bit in range(1 << n):
    for i in range(n):
        if (bit >> i) & 1:
            ans = max(ans, DP[bit][i])
print(ans)
0