結果

問題 No.357 品物の並び替え (Middle)
ユーザー UekiUeki
提出日時 2019-05-15 16:18:04
言語 Python2
(2.7.18)
結果
AC  
実行時間 1,705 ms / 5,000 ms
コード長 716 bytes
コンパイル時間 1,049 ms
コンパイル使用メモリ 6,524 KB
実行使用メモリ 6,728 KB
最終ジャッジ日時 2023-10-12 03:32:20
合計ジャッジ時間 5,498 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
5,824 KB
testcase_01 AC 11 ms
5,828 KB
testcase_02 AC 13 ms
5,860 KB
testcase_03 AC 14 ms
5,940 KB
testcase_04 AC 18 ms
5,940 KB
testcase_05 AC 17 ms
6,100 KB
testcase_06 AC 12 ms
5,960 KB
testcase_07 AC 11 ms
5,948 KB
testcase_08 AC 33 ms
5,880 KB
testcase_09 AC 367 ms
6,232 KB
testcase_10 AC 135 ms
6,184 KB
testcase_11 AC 54 ms
5,960 KB
testcase_12 AC 1,705 ms
6,728 KB
testcase_13 AC 816 ms
6,536 KB
testcase_14 AC 512 ms
6,432 KB
testcase_15 AC 29 ms
5,908 KB
testcase_16 AC 265 ms
6,112 KB
testcase_17 AC 150 ms
6,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, M = map(int, input().split())
items = [list(map(int, input().split())) for _ in range(M)]

# bitDP dp= max val of score
dp = [0]*(1 << N)

# すでに選んだ品物の集合
for mask in range(1 << N):
    for new_item in range(N):
        if mask >> new_item & 1 == 1:
            # すでに選ばれている
            continue

        tmp_add = 0
        for x, y, score in items:
            if new_item == x and mask >> y & 1 == 1:
                tmp_add += score
        new_state = mask | 1 << new_item
        dp[new_state] = max(dp[new_state], dp[mask]+tmp_add)
print(dp[-1])
0