基础01背包问题
2015-04-13 20:04
267 查看
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085
http://hihocoder.com/problemset/problem/1038?sid=320857
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数)。求背包能够容纳的最大价值。
动态转移方程 dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[i]]+w[i]);
背包九讲里面讲的很好。
http://hihocoder.com/problemset/problem/1038?sid=320857
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数)。求背包能够容纳的最大价值。
动态转移方程 dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[i]]+w[i]);
背包九讲里面讲的很好。
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long #define inf 0x7f7f7f7f #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define lowbit(x) (x)&(-x) #define Read() freopen("a.txt", "r", stdin) #define Write() freopen("b.txt", "w", stdout); #define maxn 1000000000 #define N 100010 using namespace std; int n,m; int f ; void ZeroOnePack(int cost,int weight) { for(int v=m;v>=cost;v--) f[v]=max(f[v],f[v-cost]+weight); } int main() { //Read(); //Write(); int a,b; while(~scanf("%d%d",&n,&m)) { memset(f,0,sizeof(f)); for(int i=0;i<n;i++) { scanf("%d%d",&a,&b); ZeroOnePack(a,b); } printf("%d\n",f[m]); } return 0; }
相关文章推荐
- 01背包基础-1085 背包问题
- 基础背包问题(01,完全,多重)
- 动态规划 4、基础背包问题总结(从01开始)
- poj3624 基础01背包问题 闲来无事划划水啦~
- 51nod1085---背包问题(51nod基础:01背包)
- dp入门与两个基础的背包问题
- hdu2546 饭卡 01-背包问题
- UVA 562 Dividing coins(01 背包问题)
- 01背包问题的扩展——二维费用 LeetCode 474
- 背包问题-背包01-大数据
- 洛谷P1049 装箱问题(01背包小变形)
- 01 背包问题 --- 待续 - -
- 01背包问题
- 01背包问题:初入动规坑
- 01背包和完全背包问题
- Robberies(01背包的概率问题)
- 背包问题---01背包
- 01背包问题
- 小班讲课之动态规划基础背包问题
- 01背包问题