Bone Collector(01背包)
2015-08-16 20:06
281 查看
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/N
题目:
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 ?
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
分析:
将前i件物品放入容量为j的背包中”这个子问题,若只考虑第i件物品的策略(放或不放),那么
就可以转化为一个只牵扯前i-1件物品的问题。如果不放第i件物品,那么问题就转化为“前i-1件
物品放入容量为j的背包中”,价值为f[i-1][j];如果放第i件物品,那么问题就转化为“前i-1件物品放
入剩下的容量为j-v[i]的背包中”,此时能获得的最大价值就是f[i-1][j-v[i]]再加上通过放入第i件物品获得的价值m[i] .
题目:
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 ?
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
分析:
将前i件物品放入容量为j的背包中”这个子问题,若只考虑第i件物品的策略(放或不放),那么
就可以转化为一个只牵扯前i-1件物品的问题。如果不放第i件物品,那么问题就转化为“前i-1件
物品放入容量为j的背包中”,价值为f[i-1][j];如果放第i件物品,那么问题就转化为“前i-1件物品放
入剩下的容量为j-v[i]的背包中”,此时能获得的最大价值就是f[i-1][j-v[i]]再加上通过放入第i件物品获得的价值m[i] .
#include<iostream> #include<cstring> using namespace std; int m[1005],v[1005]; int f[1005][1005]; int max(int a,int b) { if(a>b) return a; else return b; } int main() { int t,n,i,j,M; cin>>t; while(t--) { cin>>n>>M; for(i=1;i<=n;i++) cin>>m[i]; for(i=1;i<=n;i++) cin>>v[i]; for(i=1;i<=n;i++) for(j=0;j<=M;j++) { f[i][j]=(i==1?0:f[i-1][j]); if(j>=v[i]) f[i][j]=max(f[i][j],f[i-1][j-v[i]]+m[i]); } cout<<f [M]<<endl; } return 0; }
相关文章推荐
- 关于抽象类的几个简单理解
- 黑苹果系统迁移
- HDU 1848(sg博弈) Fibonacci again and again
- OC学习笔记十 copy语法
- 在Linux下安装windows后解决Linux不能启动能问题
- 千万别误解黑客
- jdk&tomcat环境变量配置及同时运行多个tomcat方法
- Gym - 100637B Lunch 规律
- heibernate增删改查总结一下自己的不足
- Onclick点击事件
- 5.适配器模式(设计模式笔记)
- 【NOIP2010TG/codevs1066】 引水入城 解题报告
- 华为OJ(在字符串中找出连续最长的数字串)
- Java心得14
- 递归实现汉诺塔问题(python)
- hdu 2896
- 让linux每天定时备份MySQL数据库并删除五天前的备份文件
- 20个高级Java面试题汇总
- hibernate之增删改查demo
- 《Java设计模式》之外观模式