51nod 1085 01背包模板
2016-01-24 20:11
281 查看
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数)。求背包能够容纳的最大价值。
#include <iostream> #include <cstring> #include <cmath> #include <string> #include<cstring> using namespace std; int const maxn = 10005; int w[maxn],c[maxn], f[maxn]; int main() { int n,v; while(cin>>n>>v) { for ( int i = 1;i <= n;i++) { cin>>c[i]>>w[i]; } memset(f,0,sizeof(f)); //不要求把背包装满,总价值尽可能大就行 /* f[0]=0; for (int i = 1;i < 1001;i++) { f[i]=-1000000; } */ //要求恰好把背包装满 for (int i = 1;i <= n;i++) { for ( int j = v;j >= c[i];j--) { f[j]=max(f[j],f[j-c[i]]+w[i]); } } cout<<f[v]<<endl; } return 0; }
相关文章推荐
- sn9c291 驱动载入成功,mpayer无法播放
- 2016年我的看电影计划
- Mac Xcode 配置OpenCv开发环境
- 【Android开发】成长篇:难忘bug
- Lucene的常用操作详解,crud
- 活动选择问题
- 利用face++的人脸检测实现的眼球跟随效果
- Eval与Bind的区别
- tornado+celery的简单使用
- 51nod 1086 多重背包模板
- Eval与Bind的区别
- [转]GeoServer地图开发解决方案(一):环境搭建篇
- 【POJ】2524 - Ubiquitous Religions(并查集,水)
- 计算机网络——传输层
- JZOJ 1769.Islands and Bridges
- 为什么要用DLL文件
- 51nod 1154回文串划分 dp+递推
- 大学毕业后拉开差距的原因
- poj 2991crane
- ubuntu下安装RemixOS双系统(Android x86)