結果

問題 No.357 品物の並び替え (Middle)
ユーザー rpy3cpprpy3cpp
提出日時 2016-04-05 00:05:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 315 ms / 5,000 ms
コード長 715 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-04-14 21:37:50
合計ジャッジ時間 2,291 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,752 KB
testcase_01 AC 29 ms
10,752 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 30 ms
10,752 KB
testcase_04 AC 31 ms
10,752 KB
testcase_05 AC 32 ms
10,752 KB
testcase_06 AC 31 ms
10,752 KB
testcase_07 AC 29 ms
10,752 KB
testcase_08 AC 34 ms
10,752 KB
testcase_09 AC 156 ms
11,136 KB
testcase_10 AC 54 ms
11,008 KB
testcase_11 AC 53 ms
10,880 KB
testcase_12 AC 315 ms
11,392 KB
testcase_13 AC 153 ms
11,136 KB
testcase_14 AC 154 ms
11,264 KB
testcase_15 AC 52 ms
10,880 KB
testcase_16 AC 84 ms
10,880 KB
testcase_17 AC 53 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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