Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40281 Accepted Submission(s): 16735
Problem Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 231).
Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1
Sample Output
14
今天上午学长讲了背包的内容,然后自己听的迷迷糊糊的,也不知道是什么东东,然后按着学长的代码敲一遍,再跟着调试的状态理解,再理解,到现在也没理解出来个所以然来....真心感觉自己的智商不够用了....
总归是有收获的,目前感觉这个题用学长的这样的方法,跟暴力枚举区别不大...............貌似就是,在条件满足(背包能装下)的情况下,一直试图更新状态,把所有的状态记录下来,然后一步步更新到最优,然后就得到全局最优解了......说的也是思路不太清晰,只知道这个程序枚举了把背包的容量一直缩小之后,所有的状态,具体为什么这么操作,还真不太懂......
动态规划,背包问题,学长讲了很多种类的背包,但是现在自己连最基本的都没听明白,自己找书看,也是看的晕乎乎的..还是慢慢的自己模拟找找感觉吧!
#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
using namespace std;
int n,s,bag[1005];//这个是动态更新的数组
struct bone
{
int val,wi;
}x[1005];
void slove()
{
int i,j;
memset(bag,0,sizeof(bag));
for(i=0;i<n;++i)//这个貌似是假设去掉哪一个的...
{
for(j=s;j>=x[i].wi;--j)//个人感觉就是暴力枚举去掉第 i 个物品后的状态
{
bag[j]=max(bag[j],bag[j-x[i].wi]+x[i].val);//找最优,就是比较去掉一个和加上另一个的最优状态..
}
}
printf("%d\n",bag[s]);//只想到最后,就把最优状态调出来了
}
int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
int a;
scanf("%d%d",&n,&s);
for(i=0;i<n;++i)
{
scanf("%d",&a);
x[i].val=a;
}
for(i=0;i<n;++i)
{
scanf("%d",&a);
x[i].wi=a;
}
slove();
}
return 0;
}
/*
2016年1月19日16:02
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
void scan(int x[],int n)
{
for(int i=0;i<n;++i)
{
scanf("%d",&x[i]);
}
}
int main()
{
int t;
//freopen("shuju.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
int n,v,wei[1005],val[1005],dp[1005]={0};
scanf("%d%d",&n,&v);
scan(val,n);scan(wei,n);
for(int i=0;i<n;++i)
{
// for(int j=wei[i];j<=v;++j)//完全背包
for(int j=v;j>=wei[i];--j)// 01背包
{
dp[j]=max(dp[j],dp[j-wei[i]]+val[i]);
}
}
printf("%d\n",dp[v]);
}
return 0;
}