結果

問題 No.2364 Knapsack Problem
ユーザー FromBooskaFromBooska
提出日時 2023-07-01 08:14:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,251 ms / 3,000 ms
コード長 1,642 bytes
コンパイル時間 351 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 143,244 KB
最終ジャッジ日時 2023-09-22 02:21:58
合計ジャッジ時間 23,335 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
76,700 KB
testcase_01 AC 76 ms
76,520 KB
testcase_02 AC 95 ms
76,748 KB
testcase_03 AC 83 ms
76,904 KB
testcase_04 AC 107 ms
77,632 KB
testcase_05 AC 135 ms
79,220 KB
testcase_06 AC 108 ms
77,688 KB
testcase_07 AC 225 ms
83,556 KB
testcase_08 AC 80 ms
76,520 KB
testcase_09 AC 276 ms
85,060 KB
testcase_10 AC 141 ms
79,288 KB
testcase_11 AC 122 ms
78,832 KB
testcase_12 AC 2,112 ms
142,956 KB
testcase_13 AC 2,071 ms
143,120 KB
testcase_14 AC 2,187 ms
142,636 KB
testcase_15 AC 2,251 ms
142,804 KB
testcase_16 AC 1,651 ms
143,056 KB
testcase_17 AC 2,189 ms
142,988 KB
testcase_18 AC 1,591 ms
143,016 KB
testcase_19 AC 2,216 ms
143,244 KB
testcase_20 AC 2,196 ms
142,740 KB
testcase_21 AC 2,065 ms
142,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# N, M < 7と小さいが、全permutaitonsは間に合わない
# NもMも使っても使わなくてもいい
# NはB降順でソートするか?
# MはD昇順でソート?、しかし、d小さいの2つより、d大きくてc大きい方がいい可能性ある
# どれを使うのかというのもあるし、どの順番で使うのか、というのもある
# トラベリングセールスマン、巡回セールスマン、ビットdp
# dp[集合sに到達済み][重さw]での最高価値
# MLE出たのでINFを下げる

N, M, W = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
D = list(map(int, input().split()))

AC = A+C
BD = B+D

# bit dp、dp[集合sに到達済み][重さw]での最高価値
INF = 10**10
NM = N+M
set_num = 1<<NM
dp = [[-INF]*(W+1) for i in range(set_num)]
dp[0][0] = 0

for s in range(set_num):
    for w in range(W+1):
        for i in range(NM):
            # 集合sに到達済みのときに、新たにiを訪れる
            if (s>>i) & 1 == 1:
                # 既にiに到達済みならばcontinue
                continue
            if i < N: #商品
                if w+AC[i] <= W:
                    dp[s|(1<<i)][w+AC[i]] = max(dp[s|(1<<i)][w+AC[i]], dp[s][w]+BD[i])
            else: #魔法
                if 0 <= w-AC[i]:
                    #print('s', s, 'w', w, 'i', i, 's|(1<<i)', s|(1<<i))
                    dp[s|(1<<i)][w-AC[i]] = max(dp[s|(1<<i)][w-AC[i]], dp[s][w]-BD[i])

ans = 0
for s in range(set_num):
    for w in range(W+1):
        ans = max(ans, dp[s][w])
print(ans)




0