Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 58027 Accepted Submission(s): 24207
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 ?
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.
1 5 10 1 2 3 4 5 5 4 3 2 1
14
|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 |
| 2 | 0 | 0 | 0 | 2 | 2 | 2 | 2 | 2 | 3 | 3 |
| 3 | 0 | 0 | 3 | 3 | 3 | 3 | 5 | 5 | 5 | 5 |
| 4 | 0 | 4 | 4 | 4 | 7 | 7 | 7 | 7 | 10 | 10 |
| 5 | 5 | 5 | 9 | 9 | 9 | 12 | 12 | 12 | 12 | 14 |
该表的第一行表示背包的大小j,第一列表示物品的价值a[I],这个表造成的疑问在于,列如为什么d[1][9]不是价值为1的物品加上价值为2的物品,他们的体积相加刚好是9啊!?这是因为在I=1这行,没有轮到价值大于1的物品来排。也就是说,第I行就是价值为1~I的物品去进行装填。按照这个道理,右下角最后一个格子里的就是我们要求的最大值。或许还是没懂,其实自己亲自把表里的值推推就会好些了。
原理懂了之后编程是水到渠成啊!
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int a[1005],b[1005];
int d[1005][1005];
int main()
{
int n;
cin>>n;
while(n--)
{
memset(d,0,sizeof(d));
int x,y;
cin>>x>>y;
for(int i=1;i<=x;i++)
cin>>a[i];///价值
for(int i=1;i<=x;i++)
cin>>b[i];///所占空间
for(int i=1;i<=x;i++)///动态规划
{
for(int j=0;j<=y;j++)
{
if(j>=b[i])
d[i][j]=d[i-1][j]>(d[i-1][j-b[i]]+a[i])?d[i-1][j]:(d[i-1][j-b[i]]+a[i]);
else
d[i][j]=d[i-1][j];
}
}
cout<<d[x][y]<<endl;
}
return 0;
}
453



被折叠的 条评论
为什么被折叠?



