【01背包+完全背包】HDU3449-Consumer

来源:互联网 发布:熊猫加速器mac版 编辑:程序博客网 时间:2024/06/02 00:24

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3449

Problem Description
FJ is going to do some shopping, and before that, he needs some boxes to carry the different kinds of stuff he is going to buy. Each box is assigned to carry some specific kinds of stuff (that is to say, if he is going to buy one of these stuff, he has to buy the box beforehand). Each kind of stuff has its own value. Now FJ only has an amount of W dollars for shopping, he intends to get the highest value with the money.
 

Input
The first line will contain two integers, n (the number of boxes 1 <= n <= 50), w (the amount of money FJ has, 1 <= w <= 100000) Then n lines follow. Each line contains the following number pi (the price of the ith box 1<=pi<=1000), mi (1<=mi<=10 the number goods ith box can carry), and mi pairs of numbers, the price cj (1<=cj<=100), the value vj(1<=vj<=1000000)
 

Output
For each test case, output the maximum value FJ can get
 

Sample Input
3 800300 2 30 50 25 80600 1 50 130400 3 40 70 30 40 35 60
 

Sample Output
210

代码:

#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int Maxn=100050;  //  数组开小了,给我报TLE;int N,W,t,m,w,v;int dp[Maxn],dp1[Maxn];int main(){    while(~scanf("%d%d",&N,&W)){        memset(dp,0,sizeof(dp));        for(int i=1;i<=N;i++){            scanf("%d%d",&t,&m);            //  这里需要注意,我们将dp的值赋值给dp1,做01背包,            //  保证每次01完全背包的全局最优解;            memcpy(dp1,dp,sizeof(dp));            for(int j=1;j<=m;j++){                scanf("%d%d",&w,&v);                //  单个盒子的01背包;                for(int k=W-t;k>=w;k--){                    dp1[k]=max(dp1[k],(dp1[k-w]+v));                }            }            //  每个盒子的完全背包;            for(int j=t;j<=W;j++){                dp[j]=max(dp[j],dp1[j-t]);            }        }        printf("%d\n",dp[W]);    }    return 0;}


0 0