51nod 1085 背包问题
2017-04-14 14:57
169 查看
[align=center]1085 背包问题 [/align]
[align=center]基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
[/align]
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数)。求背包能够容纳的最大价值。
Input
Output
Input示例
Output示例
解题思路:这就是一个简单的01背包。。。
代码如下:
[align=center]基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
[/align]
在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背包。。。
代码如下:
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; struct P{ int w,p; }a[101]; int main() { int n,m,dp[20002]; scanf("%d %d",&n,&m); for(int i=0; i<n; i++) scanf("%d %d",&a[i].w,&a[i].p); for(int i=0; i<n; i++) for(int j=m;j>=a[i].w; j--) dp[j]=max(dp[j],dp[j - a[i].w]+a[i].p); printf("%d\n",dp[m]); return 0; }
相关文章推荐
- 51Nod 1085 背包问题
- 51Nod-1085-背包问题
- 51Nod 1085 背包问题
- 51nod 1085 背包问题01
- 51Nod:1085 背包问题
- 51Nod 1085 背包问题
- 【51Nod】1085 - 背包问题(01背包)
- 51Nod 1085 背包问题(01)
- 【51Nod】1085 - 背包问题【01背包】
- 51Nod 1085 背包问题
- 51Nod-1085 背包问题
- 51nod 1085 背包问题
- 51nod--1085--背包问题
- 51nod 1085 背包问题
- 51nod 1085 背包问题【01背包】
- 【51nod 1085】背包问题(0-1背包模板)
- 51Nod 1085 背包问题(01背包 )
- 51nod 1085 背包问题
- 【51NOD-0】1085 背包问题
- [51NOD - 1085] 背包问题(动态规划)