0-1背包问题
真的好难
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 iin 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.
Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di
4 6 1 4 2 6 3 12 2 7
23
按照正确的思路写了好长时间,最后一直得不到正确的结果,可能是状态转移方程没有理解清楚,而且还涉及到空间优化的问题(人人为我)
状态转移方程真的不太好理解,方程一直在变根本就找不到它确切的值。这里先贴上别人的AC代码,以后方便查找
学0-1背包前要看的一道题
点击打开链接(神奇的口袋)
关于0-1背包的讲解
空间可以优化
空间优化后的代码
代码虐我千百遍,我待代码如初恋。