結果

問題 No.357 品物の並び替え (Middle)
コンテスト
ユーザー wgrape
提出日時 2024-10-16 15:45:07
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 940 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 254 ms
コンパイル使用メモリ 85,312 KB
実行使用メモリ 70,656 KB
最終ジャッジ日時 2026-05-05 13:31:46
合計ジャッジ時間 2,069 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# dp[s] = 今までに並べた品物の集合
# 追加でitem2を置くときに、既存の集合にitem1があれば得点が得られる

N,M = map(int,input().split())
from collections import defaultdict
dic = defaultdict(list) # item2をキーに、[item1, score]を持つ
for _ in range(M):
    item1, item2, score = map(int,input().split())
    dic[item2].append([item1, score])

dp = [0] * (1 << N)
for status in range(1 << N):
    for item2 in range(N): # 次に置くアイテム
        if (status >> item2) & 1: # 置き済み
            continue
        new_status = status | (1 << item2)
        point = 0 # item2を置いたときに得られる得点
        for item1, score in dic[item2]: # item1が置かれていれば得点が得られる
            if (status >> item1) & 1: # item1が置かれている
                point += score
        dp[new_status] = max(dp[new_status], dp[status] + point)

print(dp[-1])
0