POJ3624 0-1背包(dp+滚动数组)
2021-07-17 16:05
标签:int put describe return miss desc hat integer break Description Bessie has gone to the mall‘s jewelry store and spies a charm bracelet. Of course, she‘d like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a ‘desirability‘ factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880). Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings. Input * Line 1: Two space-separated integers: N and M Output * Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints Sample Input Sample Output 23 如果用dp[n][m]:前n个物品不超过体积m的最大价值,状态转移方程为: dp[i][j] = 0(i==0orj==0)//没有物品,背包体积再大的最大价值也是0;再多物品,背包体积为0都塞不下。 dp[i][j]=dp[i-1][j](j dp[i][j]=max(dp[i-1][j],dp[i-1][j-d[i]]+w[i])//当前背包若能装下第i个物品,那么就在装第i个物品后的最大价值和不装第i个物品的最大价值中取最大值 但由于N和M的范围太大,开二维数组会爆内存,分析求解过程发现求解dp[i][j]时只与它上一行的正上方和上一行左边的某个值有关,也就是只用到了它上面那行,可以用一个滚动的一维数组求解,把新值放到上面的位置,但要注意顺序必须是从右到左,不然会覆盖掉有用的值。 POJ3624 0-1背包(dp+滚动数组) 标签:int put describe return miss desc hat integer break 原文地址:https://www.cnblogs.com/knmxx/p/9531340.html
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 47440
Accepted: 20178
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di4 6
1 4
2 6
3 12
2 7
1 #include
文章标题:POJ3624 0-1背包(dp+滚动数组)
文章链接:http://soscw.com/index.php/essay/106235.html