51nod 1085 背包问题<水过>
2016-04-14 12:30
411 查看
1085 背包问题
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数)。求背包能够容纳的最大价值。
Input
第1行,2个整数,N和W中间用空格隔开。N为物品的数量,W为背包的容量。(1 <= N <= 100,1 <= W <= 10000)
第2 - N + 1行,每行2个整数,Wi和Pi,分别是物品的体积和物品的价值。(1 <= Wi, Pi <= 10000)
Output
输出可以容纳的最大价值。
Input示例
3 6
2 5
3 8
4 9
Output示例
14
代码:
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数)。求背包能够容纳的最大价值。
Input
第1行,2个整数,N和W中间用空格隔开。N为物品的数量,W为背包的容量。(1 <= N <= 100,1 <= W <= 10000)
第2 - N + 1行,每行2个整数,Wi和Pi,分别是物品的体积和物品的价值。(1 <= Wi, Pi <= 10000)
Output
输出可以容纳的最大价值。
Input示例
3 6
2 5
3 8
4 9
Output示例
14
代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int w[10100]; int p[10100]; int main() { int n,ww; scanf("%d%d",&n,&ww); for (int i=0;i<n;i++) scanf("%d%d",&w[i],&p[i]); int bao[10100]={0}; for (int i=0;i<n;i++) for (int j=ww;j>=0;j--) if (j>=w[i]) bao[j]=max(bao[j],bao[j-w[i]]+p[i]); printf("%d\n",bao[ww]); return 0; }
相关文章推荐
- titan实战1------介绍
- typedef和#define的用法与区别
- 京东 上台阶
- CF 467A George and Accommodation
- W3C标准以及XHTML和HTML
- 一张图看懂收录与索引的区别
- LeetCode(56)-Add Binary
- windows下MySQL 5.7.3.0安装配置图解教程(安装版)
- LeetCode(56)-Add Binary
- LeetCode(56)-Add Binary
- 部分和问题升级版
- lunix常用命令和技巧(Red Hat Enterprise Linux Server release 6.2)
- SpringMVC 400 Bad Request 问题解决
- MVC过滤器处理Session过期
- MDK 5.10 -- Reading one or more Pack descriptions failed
- 自定义控件-CascadeLayout
- 多级父子级查询
- gen_server + ets的几个小技巧
- HDU 5642 King's Order(数位dp)
- [C++]单例模式