hdu 2082 多重背包转01背包问题

原题:http://acm.hdu.edu.cn/showproblem.php?pid=2082

 1 #include<iostream>
2 #include<iomanip>
3 #include<cstdlib>
4 #include<algorithm>
5
6 #define ASIZE 27
7 #define DPSIZE 51
8
9 using namespace std;
10
11 int main(void)
12 {
13 int amount[ASIZE];
14 int dp[DPSIZE];
15 //amount和dp都是从1开始用的 为了便于统一下标
16 //因此amount[0]无意义
17
18 int m;
19 cin >> m;
20 while(m--)
21 {
22 memset(dp,0,sizeof(dp));
23
24 for(int i(1);i != ASIZE;++i)
25 cin >> amount[i];
26 dp[0] = 1;
27 for(int i(1);i != ASIZE;++i)
28 for(int j(DPSIZE-1);j != 0;--j)
29 for(int k(1);k <= amount[i] && k*i <= j;++k)
30 dp[j] += dp[j-k*i];
31 int result(0);
32 for(int i(1);i != DPSIZE;++i)
33 result += dp[i];
34 cout << result << endl;
35 }
36 return 0;
37 }

dp[j] += dp[j-k*i]的意思是dp[j]可以也只能拆成dp[j-k*i]。多重转01背包的思路。

参考资料见dd的背包九讲:http://love-oriented.com/pack/ 

posted on 2011-10-19 18:47  珞薇  阅读(260)  评论(0)    收藏  举报

导航