結果

問題 No.357 品物の並び替え (Middle)
ユーザー roarisroaris
提出日時 2019-10-29 12:37:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 119 ms / 5,000 ms
コード長 656 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 86,676 KB
実行使用メモリ 77,896 KB
最終ジャッジ日時 2023-10-12 23:20:00
合計ジャッジ時間 3,443 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
75,904 KB
testcase_01 AC 76 ms
71,016 KB
testcase_02 AC 80 ms
75,732 KB
testcase_03 AC 80 ms
75,028 KB
testcase_04 AC 84 ms
75,948 KB
testcase_05 AC 89 ms
76,232 KB
testcase_06 AC 76 ms
71,024 KB
testcase_07 AC 75 ms
71,392 KB
testcase_08 AC 91 ms
76,144 KB
testcase_09 AC 109 ms
77,468 KB
testcase_10 AC 96 ms
76,472 KB
testcase_11 AC 97 ms
76,320 KB
testcase_12 AC 119 ms
77,540 KB
testcase_13 AC 109 ms
77,228 KB
testcase_14 AC 109 ms
77,896 KB
testcase_15 AC 110 ms
77,792 KB
testcase_16 AC 105 ms
77,884 KB
testcase_17 AC 96 ms
76,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def rec(S):
    if memo[S] != -1:
        return memo[S]
    
    if S == (1<<N)-1:
        memo[S] = 0
        return 0
    
    res = 0
    
    for n in range(N):
        if not (S>>n)&1:
            add = 0
            
            for prev, score in scores[n]:
                if (S>>prev)&1:
                    add += score
                    
            res = max(res, rec(S|(1<<n))+add)
    
    memo[S] = res
    
    return res

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

for _ in range(M):
    item1, item2, score = map(int, input().split())
    scores[item2].append((item1, score))

memo = [-1] * (1<<N)

print(rec(0))
0