51Nod 1085 背包问题
来源:互联网 发布:什么是淘宝订购参谋 编辑:程序博客网 时间:2024/06/01 08:41
题意:在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数)。求背包能够容纳的最大价值。
解题思路:dp经典背包问题.dp[i][j]表示从第i个物品开始挑选总重量小于j时,总价值的最大值。递推式:dp[n][j]=0,j<w[i]时,dp[i][j]=dp[i+1][j];其余情况dp[i][j]=max(dp[i+1][j],dp[i+1][j-w[i]]+v[i]),也就是第i个物品选与不选的两种情况。要注意的是数组的范围,数组开小了居然是TLE。。。
代码:
#include <iostream>#include <algorithm>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <vector>#include <set>#include <map>#include <queue>using namespace std;const int maxn=110;const int maxm=10010;int dp[maxn][maxm];int w[maxn],v[maxn];int n,m;void DP(){ for(int i=n-1;i>=0;i--) { for(int j=1;j<=m;j++) { if(j<w[i])dp[i][j]=dp[i+1][j]; else dp[i][j]=max(dp[i+1][j],dp[i+1][j-w[i]]+v[i]); } } printf("%d\n",dp[0][m]);}int main(){ while(scanf("%d%d",&n,&m)==2) { for(int i=0;i<n;i++) { scanf("%d%d",&w[i],&v[i]); } for(int j=0;j<=m;j++) { dp[n][j]=0; } DP(); } return 0;}
阅读全文
0 0
- 51nod 1085 背包问题
- 51nod 1085 背包问题
- 51Nod--1085背包问题
- 51nod 1085 背包问题
- 51Nod 1085 背包问题
- 51Nod 1085 背包问题
- 51nod 1085 背包问题
- 51nod 1085 背包问题
- 【51nod 1085 背包问题】
- 51nod 1085 背包问题
- 51Nod-1085 背包问题
- 51nod--1085--背包问题
- 51nod 1085 背包问题
- 51Nod 1085 背包问题
- 51nod 1085 背包问题【01背包】
- 51nod 1085 背包问题【01背包】
- 51nod-1085 背包问题【01背包】
- 背包问题-----51Nod
- JAVA:0到1000内完数
- 堆排序
- tar命令详解
- ConcurrentModificationException
- Linux系统的进程通常有以下几种状态
- 51Nod 1085 背包问题
- 数据库_数据库设计1
- CodeForces
- populating-next-right-pointers-in-each-node
- wkhtmltopdf 设置 cookie
- C++实现双向链表
- skb
- Java高并发,如何解决,什么方式解决
- 闲谈团队的代码质量:没有code review的团队没有未来