51nod 1085 01背包
来源:互联网 发布:淘宝去痘印产品 编辑:程序博客网 时间:2024/06/07 15:32
1085 背包问题
在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
题解:
01背包问题。紫书p265
状态转移方程
dp[i][j]=max(dp[i][j],dp[i-1][j-bag[i].w]+bag[i].p)
代码:
#include <iostream>using namespace std;#define INF 1<<30const int maxn=100+10;struct{ int w; int p;}bag[maxn];int dp[maxn][maxn];int main(){ int n,s; cin>>n; cin>>s; for(int i=1;i<=n;i++) cin>>bag[i].w>>bag[i].p; for(int i=1;i<=n;i++) for(int j=0;j<=s;j++) { dp[i][j] = (i==1?0:dp[i-1][j]); if(j>=bag[i].w) { dp[i][j]=max(dp[i][j],dp[i-1][j-bag[i].w]+bag[i].p); } } cout<<dp[n][s]<<endl; return 0;}
阅读全文
0 0
- 51nod 1085 背包问题【01背包】
- 51nod 1085 背包问题【01背包】
- 51nod-1085 背包问题【01背包】
- 51nod 1085 01背包
- 51nod 1085 01背包模板
- 51nod 1085 背包问题01
- 51Nod 1085 背包问题(01)
- 51Nod 1085 背包问题(01背包 )
- 【51Nod】1085 - 背包问题(01背包)
- 51nod 1085 背包问题(01背包)
- 51nod 1085 背包问题
- 51nod 1085 背包问题
- 51Nod--1085背包问题
- 51nod 1085 背包问题
- 51Nod 1085 背包问题
- 51Nod 1085 背包问题
- 51nod 1085 背包问题
- 51nod 1085 背包问题
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
- java反射机制动态设置类的属性值
- redis在windows上应用和部署
- JavaScript权威指南读书笔记——JavaScript的扩展
- 页面传值中文乱码
- 51nod 1085 01背包
- python Anaconda2安装OpenCV2
- 300020528564
- CodeChef:Travel to all Points(二分 & 技巧)
- DWZ-Dialog调整位置
- 方法交换!!! o-c
- eclipse常用快捷键,持续更新
- Java中读取txt文本中内容+目录文件复制到指定目录
- php二维数组相同id的数量相加