yl-可恶的背包
\boxed{\text{前言}}
writer,coder,jcer all from
你们最喜欢的英文题面又回来了 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:
- Take it
- Not take it
- Not enough money (which merges with "not take it")
So effectively, two cases:
- Take (1)
- Not take (0)
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:
为什么
对于不选的情况,这个物品不会放进背包,所以和剩余容量不够的情况是一样的是
答案是
完了~
完全背包
题目
你又要去商店买东西,但是老板太富了,每个东西都有无数个,你知道每个商品的价格
v_i 和好吃度{w1}_i ,你只有w 块钱,你最多能获得多少好吃程度
其实和01背包一样,刚才怕重复,这次要重复
多重背包
题目
你又双叕要去商店买东西,老板是个普通人,每个东西都有
g_i 个,你知道每个商品的价格v_i 和好吃度{w1}_i ,你只有w 块钱,你最多能获得多少好吃程度
其实和01背包一样,对于每个