結果

問題 No.357 品物の並び替え (Middle)
コンテスト
ユーザー rpy3cpp
提出日時 2016-04-05 00:05:19
言語 Python3
(3.15.2 + numpy 2.5.4 + scipy 1.18.1 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 213 ms / 5,000 ms
+ 670µs
コード長 715 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 70 ms
コンパイル使用メモリ 14,848 KB
実行使用メモリ 11,904 KB
最終ジャッジ日時 2026-10-15 12:39:33
合計ジャッジ時間 2,917 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def read_data():
    N, M = map(int, input().split())
    scores = [[0] * N for n in range(N)]
    for m in range(M):
        a, b, c = map(int, input().split())
        scores[a][b] = c
    return N, scores


def solve(N, scores):
    dp = [0] * (1 << N)
    for i in range(1, 1 << N):
        dp[i] = find_max(i, dp, scores, N)
    return dp[-1]

def find_max(used, dp, scores, N):
    record = 0
    for j in range(N):
        if used & (1<<j):
            score = dp[used - (1 << j)]
            sj = scores[j]
            score += sum(sj[b] for b in range(N) if not used & (1<<b))
            if score > record:
                record = score
    return record

N, scores = read_data()
print(solve(N, scores))
0