yl-可恶的背包

· · 算法·理论

\boxed{\text{前言}}

writer,coder,jcer all from \textcolor{#52c41a}{\mathrm{Luna\_From\_Starry}}

你们最喜欢的英文题面又回来了 E:

Dynamic Programming is an algorithm that breaks down complex problems into overlapping subproblems and stores the solutions to these subproblems to avoid redundant calculations. This time, we focus on Knapsack DP, which, as the name suggests, involves packing items into a knapsack. It has a large family of variations.

0-1 Knapsack

The 0-1 knapsack is the most classic knapsack problem, so it gets the most detailed explanation.

Problem

You go to a store to buy items, but the owner is so poor that each item is available only once. You know the price (v_i) and the tastiness (w_i) of each item. You only have (w) dollars. What is the maximum total tastiness you can obtain?

Some might say, "Just calculate the best combination!" But remember, the most cost-effective option might not use all your money, and spending all your money might be the best strategy. Hence, we need 0-1 knapsack.

Let (dp_{i, j}) represent the maximum value achievable considering the first (i) items with a knapsack capacity of (j).

For each item (i), there are three cases:

So effectively, two cases:

For the "take" case, the remaining capacity decreases by the item's weight, and the value increases by the item's value. Thus, the value is (dp{i, j} = dp{i - 1, j - w_i} + v_i).

Why subtract 1 from (i)? To avoid taking the same item multiple times.

For the "not take" case, the item is not included, so the value remains (dp{i, j} = dp{i - 1, j}).

The answer is (dp_{n, m}).

Done~

Unbounded Knapsack

Problem

You go to the store again, but now the owner is rich and each item is available in unlimited quantities. You know the price (v_i) and tastiness (w_i) of each item. You only have (w) dollars. What is the maximum total tastiness you can obtain?

This is similar to the 0-1 knapsack, but now items can be taken multiple times.

Bounded Knapsack

Problem

You go to the store once more, and the owner is ordinary—each item has (g_i) copies. You know the price (v_i) and tastiness (w_i) of each item. You only have (w) dollars. What is the maximum total tastiness you can obtain?

This is also similar to the 0-1 knapsack. For each ((i, j)), just enumerate (k) (the number of copies taken).

C:

这次上场的是**背包dp**,顾名思义,就是往背包里塞东西,但是他有一个庞大的家族 ## 01背包 0-1背包是最最最经典的背包,所以也是最长的 #### 题目 > 你要去商店买东西,但是老板太穷了,每个东西都只有一个,你知道每个商品的价格$v_i$和好吃度${w1}_i$,你只有$w$块钱,你最多能获得多少好吃程度 这时候就有小盆友说了,啊这直接计算(~忘词了~)就行了呀,为什么要什么dp啊 主播告诉你,肯定是要的,因为最划算的有可能不能让钱包空空,而把钱全花光是最好的选择,所以就有了01背包 设$dp_{i, j}$表示表示第$ i $个物品在背包容量为$ j $的最大价值。 在每一个商品$i$中,都有三种情况 - 选 - 不选 - 没那么多钱(省下的钱) 而第二种和第三中可以合并 所以只有两种 - 要(1) - 不要(0) 对于选的情况,背包的剩余容量会减小这个物品需要的容量,价值会增加这个物品的价值,故此情况下的价值为 $dp_{i, j} = dp_{i - 1, j - w_i} + v_i

为什么i要减1呢,因为当前i有可能选两次

对于不选的情况,这个物品不会放进背包,所以和剩余容量不够的情况是一样的是 dp_{i, j} = dp_{i - 1, j}

答案是dp_{n, m}

完了~

完全背包

题目

你又要去商店买东西,但是老板太富了,每个东西都有无数个,你知道每个商品的价格v_i和好吃度{w1}_i,你只有w块钱,你最多能获得多少好吃程度

其实和01背包一样,刚才怕重复,这次要重复

多重背包

题目

你又双叕要去商店买东西,老板是个普通人,每个东西都有g_i个,你知道每个商品的价格v_i和好吃度{w1}_i,你只有w块钱,你最多能获得多少好吃程度

其实和01背包一样,对于每个(i, j)在枚举一个