N - 01背包

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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
 
 
题解:0-1背包问题,dp[i][j]表示i个物品最大价值j
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[][];
int a[],b[];
int main()
{
int t,n,v;
cin>>t;
while(t--)
{
cin>>n>>v;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=;j<=v;j++)
{
dp[i][j]=(i==?:dp[i-][j]);
if(j>=b[i])
dp[i][j]=max(dp[i-][j],dp[i-][j-b[i]]+a[i]);
}
}
printf("%d\n",dp[n][v]);
}
return ;
}

HDU2602 (0-1背包问题)的更多相关文章

  1. 蓝桥杯 0/1背包问题 (java)

      今天第一次接触了0/1背包问题,总结一下,方便以后修改.不对的地方还请大家不啬赐教! 上一个蓝桥杯的例题: 数据规模和约定 代码: import java.util.Scanner; public ...

  2. 经典递归问题:0,1背包问题 kmp 用遗传算法来解背包问题,hash表,位图法搜索,最长公共子序列

    0,1背包问题:我写笔记风格就是想到哪里写哪里,有很多是旧的也没删除,代码内部可能有很多重复的东西,但是保证能运行出最后效果 '''学点高大上的遗传算法''' '''首先是Np问题的定义: npc:多 ...

  3. Java实现动态规划法求解0/1背包问题

    摘要: 使用动态规划法求解0/1背包问题. 难度: 初级 0/1背包问题的动态规划法求解,前人之述备矣,这里所做的工作,不过是自己根据理解实现了一遍,主要目的还是锻炼思维和编程能力,同时,也是为了增进 ...

  4. HDU-1864&&HDU-2602(01背包问题)

    DP-01背包问题例题 输入处理有点恶心人,不过处理完后就是简单的DP了 从头开始dp[i]表示从0开始到i的最优结果,最后从都边里dp数组,求得最大的报销额. 对于每个i都要从头维护最优结果.(二刷 ...

  5. 0/1背包问题(DP)

    Description 给定 n 个物品和一个背包.物品 i 的重量是 wi ,其价值为 vi ,背包的容量为 C .问:应该如何选择装入背包的物品,使得装入背包中物品的总价值最大? Input 输入 ...

  6. hdu2602Bone Collector ——动态规划(0/1背包问题)

    Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collec ...

  7. 【Python】0/1背包、动态规划

    0/1背包问题:在能承受一定重量的背包中,放入重量不同,价值不同的几件物品,怎样放能让背包中物品的价值最大? 比如,有三件物品重量w,价值v分别是 w=[5,3,2] v=[9,7,8] 包的容量是5 ...

  8. 使用LINGO来解决0/1背包算法问题

    1.问题说明 0/1背包问题:我们有n种物品,物品j的重量为wj,价格为pj.我们假定所有物品的重量和价格都是非负的.背包所能承受的最大重量为W.如果限定每种物品只能选择0个或1个,则问题称为0-1背 ...

  9. HDU 2602 Bone Collector 0/1背包

    题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  10. 动态规划-背包问题 Knapsack

    2018-03-15 13:11:12 背包问题(Knapsack problem)是一种组合优化的NP完全问题.问题可以描述为:给定一组物品,每种物品都有自己的重量和价格,在限定的总重量内,我们如何 ...

随机推荐

  1. poj3693 Maximum repetition substring

    题意 给出一个长度为\(n(n\leqslant 100000)\)的串,求一个字典序最小的子串使得它是某个字符串重复\(k\)次得到的,且\(k\)最大 题解 后缀数组论文上的题,跟上一篇uva那个 ...

  2. 使用BSD socket编写Windows版的网络程序

    我们知道BSD Socket是标准的套接字规范,那么怎么在windows使用他们呢? 我们首先要引用<winsock2.h>和ws2_32.lib 然后,执行WSAStartup #ifd ...

  3. 用户浏览器关闭cookie处理方法

    方法一: function getSessionId(){ var c_name = "jsessionid"; // alert("cookie:"+docu ...

  4. DataTable无法使用AsEnumerable ()的解决办法

    本人定义了DataSet后将表1赋给datatable,在写linq时调用datatable.asenumerable(),但报datatable不包含asenumerable的定义,求高手指点.Sy ...

  5. todoing

    1.如果系类没有数据需要返回么? 2.需要增加系列的门店打点么?

  6. 【Linux常用工具】1.1 diff命令的三种格式

    diff是用来比较两个文本文件的差异的工具,它有三种格式,下面用实例介绍一下: 准备三个测试文件1.txt 2.txt 3.txt bixiaopeng@bixiaopengtekiMacBook-P ...

  7. Java 调用Dll

    Java 中怎么能调用到dll中的函数呢? 关键是java中生的本地函数名參数和dll中的本地函数名參数一模一样. 这个程序是java中调用dll中的求和函数. 一,java代码部分操作 1.新建pr ...

  8. CHAR 详解

    CHAR(20):20指的是表中的a字段能存储的最大字符个数 CREATE TABLE `a` ( `a` char(20) DEFAULT NULL) ENGINE=InnoDB DEFAULT C ...

  9. 查看pid

    可以使用ps -ef | grep httpd查看PID 然后kill –l PID

  10. Markdown写接口文档,自动添加TOC

    上回说到,用Impress.js代替PPT来做项目展示.这回换Markdown来做接口文档好了.(不敢说代替Word,只能说个人感觉更为方便)当然,还要辅之以Git,来方便版本管理. Markdown ...