本文主要是介绍杭电2602-Bone Collector,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20964 Accepted Submission(s): 8388
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/* 下面是思路的基本过程 问题的特点是:每种物品一件,可以选择放1或不放0。 用子问题定义状态:即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是: f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]} 这个方程非常重要,据说基本上所有跟背包相关的问题的方程都是由它衍生出来的。所以详细的查了一下这个方程的含义:“将前i件物品放入容量为v的背包中”这个子问题,若只考虑第i件物品的策略(放或不放),那么就可以转化为一个只牵扯前i-1件物品的问题。如果不放第i件物品,那么问题就转化为“前i-1件物品放入容量为v的背包中”,价值为f[i-1][v];如果放第i件物品,那么问题就转化为“前i-1件物品放入剩下的容量为v-c[i]的背包中”,此时能获得的最大价值就是f[i-1][v-c[i]]再加上通过放入第i件物品获得的价值w[i]。 在有的地方看到的背包问题题目中,有两种不太相同的问法。有的题目要求“恰好装满背包”时的最优解,有的题目则并没有要求必须把背包装满。一种区别这两种问法的实现方法是在初始化的时候有所不同。 如果是第一种问法,要求恰好装满背包,那么在初始化时除了f[0]为0其它f[1..V]均设为-∞,这样就可以保证最终得到的f[N]是一种恰好装满背包的最优解。 如果并没有要求必须把背包装满,而是只希望价格尽量大,初始化时应该将f[0..V]全部设为0。 为什么呢?可以这样理解:初始化的f数组事实上就是在没有任何物品可以放入背包时的合法状态。如果要求背包恰好装满,那么此时只有容量为0的背包可能被价值为0的nothing“恰好装满”,其它容量的背包均没有合法的解,属于未定义的状态,它们的值就都应该是-∞了。如果背包并非必须被装满,那么任何容量的背包都有一个合法解“什么都不装”,这个解的价值为0,所以初始时状态的值也就全部为0了。 小结 01背包问题是最基本的背包问题,它包含了背包问题中设计状态、方程的最基本思想,另外,别的类型的背包问题往往也可以转换成01背包问题求解。故仔细体会上面基本思路的得出方法,状态转移方程的意 */ #include<iostream> const int MAX=1001; int DP[MAX][MAX];//DP[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值 int jiazhi[MAX]; int tiji[MAX]; using namespace std; int main() {int t,n,v,i,j;cin>>t;while(t--){cin>>n>>v;for(i=1;i<=n;i++)cin>>jiazhi[i];for(i=1;i<=n;i++)cin>>tiji[i];memset(DP,0,sizeof(DP));for(i=1;i<=n;i++){for(j=0;j<=v;j++)//这里注意!!骨头体积可以为0,背包容量也可以为0 {if(j>=tiji[i]){if(DP[i-1][j]<(DP[i-1][j-tiji[i]]+jiazhi[i]))DP[i][j]=DP[i-1][j-tiji[i]]+jiazhi[i];elseDP[i][j]=DP[i-1][j];}elseDP[i][j]=DP[i-1][j];}}cout<<DP[n][v]<<endl;}return 0; }
这篇关于杭电2602-Bone Collector的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!