HDU 2602 Bone Collector
2016-05-19 20:34
295 查看
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 47894 Accepted Submission(s): 19983
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 ?
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 231).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
Autho
Teddy
Source
HDU 1st “Vegetable-Birds
Cup” Programming Open Contest
不错的动态规划详解!!! (点击链接)
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<climits> #include<string> #include<queue> #include<stack> #include<set> #include<map> #include<algorithm> using namespace std; #define rep(i,j,k)for(i=j;i<k;i++) #define per(i,j,k)for(i=j;i>k;i--) #define MS(x,y)memset(x,y,sizeof(x)) #define max(a,b) a>b?a:b #define min(a,b) a<b?a:b #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long LL; const int INF=0x7ffffff; #define lson rt<<1, l, m #define rson rt<<1|1, m+1, r int dp[1000][1000]; int main() { int t,n,v,i,j; int va[1000],vo[1000]; cin>>t; while(t--) { cin>>n>>v; for(i=1;i<=n;i++) cin>>va[i]; for(i=1;i<=n;i++) cin>>vo[i]; memset(dp,0,sizeof(dp));//初始化操作 for(i=1;i<=n;i++){ for(j=0;j<=v;j++){ if(vo[i]<=j)//表示第i个物品将放入大小为j的背包中 dp[i][j]=max(dp[i-1][j],dp[i-1][j-vo[i]]+va[i]);//第i个物品放入后,那么前i-1个物品可能会放入也可能因为剩余空间不够无法放入 else //第i个物品无法放入 dp[i][j]=dp[i-1][j]; } } cout<<dp [v]<<endl; } return 0; }
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<climits> #include<string> #include<queue> #include<stack> #include<set> #include<vector> #include<map> #include<algorithm> using namespace std; #define rep(i,j,k)for(i=j;i<k;i++) #define per(i,j,k)for(i=j;i>k;i--) #define MS(x,y)memset(x,y,sizeof(x)) typedef long long LL; const int INF =0x7FFFFFFF; const int low(int x){return x&-x;} const int M=1000+1; int val[M],vol[M]; int flag[M][M]; int t,i,j,n,k,m,v; int F(int v,int n) { int remax; if(flag[v] !=-1)remax=flag[v] ; else if(n==0){ if(v>=vol )remax=val ; else remax=0; } else if(v>=vol ) remax=max(F(v,n-1),F(v-vol ,n-1)+val ); else remax=F(v,n-1); flag[v] =remax; return remax; } int main() { scanf("%d",&t); while(t--){ cin>>n>>v; rep(i,0,n)scanf("%d",&val[i]); rep(i,0,n)scanf("%d",&vol[i]); MS(flag,-1); printf("%d\n",F(v,n-1)); } return 0; }
相关文章推荐
- Notification图标总是显示应用的图标(设置smallIcon无效)
- 初始化参数
- 初始化参数
- 第10周表格
- WF 2016题解(待填坑)
- java 之DelayQueue实际运用示例
- linux防火墙开发实例 获取FTP账号密码
- nyoj 169 素数 第三届河南省程序设计大赛
- CSS属性选择器温故-4
- JQuery 多属性选择节点
- 设计模式之抽象工厂模式
- 斐波那契数列的数学分析
- 一个数组实现两个栈
- Block从简单到高级的使用,以及项目中经常使用的场景
- JAXP对XML文档进行DOM解析实现增删改
- 数值调节按钮
- M进制与N进制之间的转换
- AngularJS控制器详情
- 数据库连接池C3P0最常用配置
- Ubuntu下类似 Mac下的 Quicksilver, Alfred 的快捷键工具