結果

問題 No.357 品物の並び替え (Middle)
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-08-09 15:41:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 362 ms / 5,000 ms
コード長 749 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-04-24 23:03:30
合計ジャッジ時間 2,369 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,624 KB
testcase_01 AC 29 ms
10,752 KB
testcase_02 AC 32 ms
10,752 KB
testcase_03 AC 35 ms
10,624 KB
testcase_04 AC 34 ms
10,624 KB
testcase_05 AC 33 ms
10,752 KB
testcase_06 AC 30 ms
10,624 KB
testcase_07 AC 30 ms
10,752 KB
testcase_08 AC 36 ms
10,752 KB
testcase_09 AC 174 ms
11,136 KB
testcase_10 AC 58 ms
10,624 KB
testcase_11 AC 58 ms
10,752 KB
testcase_12 AC 362 ms
11,264 KB
testcase_13 AC 172 ms
10,880 KB
testcase_14 AC 174 ms
11,008 KB
testcase_15 AC 58 ms
10,752 KB
testcase_16 AC 94 ms
10,880 KB
testcase_17 AC 59 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3


def solve(n, table):
    dp = [0 for _ in range(1 << n)]
    for s in range(1 << n):
        for next_item in range(n):
            if s & (1 << next_item) != 0:
                continue
            delta = sum(table[prev_item][next_item]
                        for prev_item in range(n) if s & (1 << prev_item))
            dp[s | (1 << next_item)] = max(
                dp[s | (1 << next_item)], dp[s] + delta)
    return dp[(1 << n) - 1]


def main():
    n, m = map(int, input().split())
    table = [[0 for _ in range(n)] for _ in range(n)]
    for _ in range(m):
        i1, i2, s = map(int, input().split())
        table[i1][i2] = s
    ans = solve(n, table)
    print(ans)


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