这道题目就开始01背包问题了,emmmm,刚开始确实很难懂o(╥﹏╥)o
题目:
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 ?
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.
Output
One integer per line representing the maximum of the total value (this number will be less than 2^31).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
日常翻译:
许多年前,在泰迪的家乡,有一个人被称为“骨头收藏家”。这个男人喜欢收集各种各样的骨头,比如狗的,牛的,还有他去了坟墓…
骨收集器有一个体积为V的大袋子,沿着他的收集之旅有很多骨头,显然,不同的骨骼有不同的价值和不同的体积,现在根据他的行程给出每个骨头的值,你能计算吗?超出骨收集器可以获得的总价值的最大值?
输入
第一行包含整数T,个案数。
接下来是T个案例,每个案例有三行,第一行包含两个整数N,V,(N <= 1000,V <= 1000)表示骨骼的数量和他的包的体积。第二行包含表示每个骨骼值的N个整数。第三行包含表示每个骨骼体积的N个整数。
产量
每行一个整数表示总值的最大值(该数字将小于2 ^ 31)。
样本输入
1
5 10
1 2 3 4 5
5 4 3 2 1
样本输出
14
思路:这道题是个01背包的问题,首先给出关系好吧(╯▽╰),然后这里存在一个困扰:如果有多余空间去放置的话,那么放置肯定比不放的价值大啊,因为我多放入一个物品了啊,也就是说选择不放的函数f(n−1,x),f(n−1,x)肯定小与放置的函数f(n−1,c−w)+v,说道这里你就犯了一个惯性错误,认为f(n−1,x)与f(n−1,c−w)所对应的F(n,c,x)中的x={x1,x2,…xn}组合相同,在理解这个函数的意义:从n个物品里面选物品,容量为C,能达到的最大价值。这个两个函数组合不一定相同,为容量C约束条件变了。就会变为从n-1个物品里面选,容量为C与从n-1个物品里面选,容量为c-w且加上v,两者哪个大。彻底理解了函数f之后,我相信没有什么能够阻挡你学习这个算法了
f[i][j]表示前i个物品,背包大小为j时,可获得的最大价值。
对于物品i
f[i][j]=max(