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
SampleInput
3 800
300 2 30 50 25 80
600 1 50 130
400 3 40 70 30 40 35 60
SampleOutput
210

这个算法的思想为设立一个dp[i][j],j上面的数字表示已经花费的金额,i代表背包标号。dp[i][j]表示花费j金额时获得的价值
t(t>=j)为初始拥有的金钱是,准确的来讲dp[i][j]到dp[i][t]都等于dp[i][j]为花费j金额时获得的价值
这里又要纠正一个误区,当开始想这道题的时候想着分成一个盒子的情况和两个盒子的情况,依次类推。。。。
这就是没有动态规划的思想,用动态规划的思想是依次遍历每个箱子,用一个dp[][]来记录此时的最大价值,随着不断遍历每个箱子
这个dp[][]的值也会不断的得到更新(用max()函数更新)等到把所有的箱子遍历好后就是最优解。
dp遍历方式类似广度优先的遍历。
 #include<stdio.h>
#include<string.h>
#define wsize 100010
#define nsize 65
#define msize 11
int dp[nsize][wsize];
int n,t,box,num;
int max(int a,int b)
{
return a>b?a:b;
} int main ()
{
int i,j,k;
while(scanf("%d %d",&n,&t)!=EOF)
{
memset(dp,-,sizeof(dp)); //有依赖关系,要赋初值-1
memset(dp[],,sizeof(dp[])); //对于二维数组memset如果用dp则表示把二维数组的所有值赋值,如果用dp[0]则表示把第0行赋值
for(i=;i<=n;i++)
{
scanf("%d %d",&box,&num);
for(k=box;k<=t;k++)
dp[i][k]=dp[i-][k-box]; //先让i层买盒子,因为盒子没有价值,所以直接等于上一层的花费+盒子钱
for(j=;j<num;j++) //在已花费盒子钱的基础上,此时再对dp[i]层做01背包,即i层一个盒子多种物品的最大价值
{
int c,w;
scanf("%d %d",&c,&w);
for(k=t;k>=c;k--)
{
if(dp[i][k-c]!=-) //因为开始的时候box到t都已经赋值,这个是钱的范围,这时小于box的地方全是-1即不买大盒子是不能装有价值的小盒子
dp[i][k]=max(dp[i][k],dp[i][k-c]+w); // 这里不能dp[i][k]=max(dp[i][j],dp[i][k-box-c]+w)
//因为已经买过盒子了,这个表达式代表一个盒子基础上一个物品带一个盒子
}
}
for(j=;j<=t;j++)
{ // 当遍历完一个箱子以后,要把花费不用钱的价值和之前盒子作比较,取两者中最大的即局部最优,然后等待和下一组进行比较
dp[i][j]=max(dp[i][j],dp[i-][j]);
}
}
printf("%d\n",dp[n][t]);
}
return ;
}

最新文章

  1. apue第四章学习总结
  2. Useful for Android the development engineer from Github
  3. 你所不知道的五件事情--java.util.concurrent(第二部分)
  4. jQuery语法总结及注意事项
  5. Transaction: atomicity, consistency, separability, persistence
  6. 【Trie】【HDU1247】【Hat’s Wordsfd2】
  7. 打印 PHP $_SERVER 常量
  8. R 语言开发环境搭建
  9. ExceL转PDF
  10. 演讲小技巧iPhone+Keynote
  11. Andorid基础_web通信_webView案例
  12. jquery向Django后台发送数组
  13. git基本操作1
  14. 「浙大ACM」图森未来杯游记一篇以及简易口胡题解
  15. react组件通信那些事儿
  16. Day8 函数指针做函数参数
  17. 编写jsp动态网页
  18. es学习-索引管理
  19. Spring ContextLoaderListener
  20. 战火魔兽CJQ圣印问题

热门文章

  1. navicat ora-28547:connection to server failed
  2. eclipse no java machine vitual was found
  3. Content-Length实体的大小
  4. jQuery 源码学习笔记
  5. Oracle的PLSQL别名中文出现乱码解决方法
  6. login.jsp
  7. 各大主流.Net的IOC框架
  8. js setAttribute removeAttribute
  9. Python websocket
  10. Unix环境高级编程(十八)高级进程间通信