結果

問題 No.357 品物の並び替え (Middle)
ユーザー みあえみあえ
提出日時 2024-04-01 01:29:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 130 ms / 5,000 ms
コード長 757 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 76,020 KB
最終ジャッジ日時 2024-04-01 01:29:56
合計ジャッジ時間 2,407 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
64,316 KB
testcase_01 AC 44 ms
55,616 KB
testcase_02 AC 51 ms
62,092 KB
testcase_03 AC 50 ms
62,092 KB
testcase_04 AC 65 ms
64,316 KB
testcase_05 AC 54 ms
64,316 KB
testcase_06 AC 47 ms
61,464 KB
testcase_07 AC 42 ms
55,616 KB
testcase_08 AC 59 ms
66,380 KB
testcase_09 AC 98 ms
76,020 KB
testcase_10 AC 68 ms
72,564 KB
testcase_11 AC 67 ms
70,508 KB
testcase_12 AC 130 ms
75,880 KB
testcase_13 AC 103 ms
76,020 KB
testcase_14 AC 102 ms
76,020 KB
testcase_15 AC 70 ms
72,712 KB
testcase_16 AC 82 ms
75,772 KB
testcase_17 AC 69 ms
72,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from itertools import combinations


def solve(N, M, items2score):
    dp = [-float("inf")] * (1 << N)
    dp[0] = 0
    for V in range(1 << N):
        for i in range(N):
            if (V >> i) & 1:
                continue
            s = 0
            for j in range(N):
                if (V >> j) & 1:
                    s += items2score[(j, i)]
            VV = V | (1 << i)
            dp[VV] = max(dp[VV], dp[V] + s)

    return dp[-1]


def main():
    N, M = map(int, input().split())
    items2socre = defaultdict(int)
    for _ in range(M):
        i, j, s = map(int, input().split())
        items2socre[(i, j)] = s
    ans = solve(N, M, items2socre)
    print(ans)


if __name__ == "__main__":
    main()
0