POJ3624 Charm Bracelet(01背包)
2015-12-26 19:55
295 查看
Description
Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from theN (1 ≤N ≤ 3,402) available charms. Each charmi in the supplied list has a weightWi
(1 ≤Wi ≤ 400), a 'desirability' factorDi (1 ≤Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more thanM (1 ≤M ≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers:Wi andDi
Output
* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
Sample Input
Sample Output
01背包(ZeroOnePack): 有N件物品和一个容量为V的背包。(每种物品均只有一件)第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。
这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。
那么可以将问题转化为“第i件物品放或不放”这样的子问题。
1、第i件物品不放:前i-1件物品放入v容量的背包中价值总和
2、第i件物品不放:前i-1件物品放入v-Ci容量的背包中的价值总和加上i件物品的价值Wi
状态转移方程就为F[i,v]=max{F[i-1,v],F[i-1,v-Ci]+Wi};
这里是使用二维数组存储的,具体的代码如下,不过我交了之后发现MLE了,那么就要使用一维数组存储,下面我会具体讲述的。
用二维数组存储还有一个坑点就是一定要加
而如果j直接从W[I]—>m 的话
例如这道题里i=3,j=4这个状态就会出错,因为i=2,j=1时dp[2][3]=0(应该为4),dp[3][4]=12(应该为16)。
既然二维数组存储那么废柴,那么当然要用一维数组啦。
状态方程是这样的:F[v]=max{F[v],F[v-Ci]+Wi};
为什么能用一维数组存储呢?为了方便理解,我画了一张图。
我发现当内循环是逆序时,就可以保证后一个max{}中的F[v]和F[v-Ci]+Wi是前面状态的最优放法,也避免了二维数组存储的坑点。
代码如下:
Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from theN (1 ≤N ≤ 3,402) available charms. Each charmi in the supplied list has a weightWi
(1 ≤Wi ≤ 400), a 'desirability' factorDi (1 ≤Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more thanM (1 ≤M ≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers:Wi andDi
Output
* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
Sample Input
4 6 1 4 2 6 3 12 2 7
Sample Output
23
01背包(ZeroOnePack): 有N件物品和一个容量为V的背包。(每种物品均只有一件)第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。
这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。
那么可以将问题转化为“第i件物品放或不放”这样的子问题。
1、第i件物品不放:前i-1件物品放入v容量的背包中价值总和
2、第i件物品不放:前i-1件物品放入v-Ci容量的背包中的价值总和加上i件物品的价值Wi
状态转移方程就为F[i,v]=max{F[i-1,v],F[i-1,v-Ci]+Wi};
这里是使用二维数组存储的,具体的代码如下,不过我交了之后发现MLE了,那么就要使用一维数组存储,下面我会具体讲述的。
#include<iostream> #include<cstring> using namespace std; int dp[3403][12881]; int main(void) { int i,j,n,m,W[4000],D[4000]; cin>>n>>m; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) cin>>W[i]>>D[i]; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { dp[i][j]=dp[i-1][j]; if(j>=W[i]) dp[i][j]=max(dp[i-1][j],dp[i-1][j-W[i]]+D[i]); } cout<<dp [m]<<endl; return 0; }
用二维数组存储还有一个坑点就是一定要加
dp[i][j]=dp[i-1][j];这一句,不然容量比第i件物品容量小的状态下的最大价值,变成了第i件物品的价值
而如果j直接从W[I]—>m 的话
例如这道题里i=3,j=4这个状态就会出错,因为i=2,j=1时dp[2][3]=0(应该为4),dp[3][4]=12(应该为16)。
既然二维数组存储那么废柴,那么当然要用一维数组啦。
状态方程是这样的:F[v]=max{F[v],F[v-Ci]+Wi};
为什么能用一维数组存储呢?为了方便理解,我画了一张图。
我发现当内循环是逆序时,就可以保证后一个max{}中的F[v]和F[v-Ci]+Wi是前面状态的最优放法,也避免了二维数组存储的坑点。
代码如下:
#include<iostream> #include<cstring> using namespace std; int main(void) { int i,j,n,m,W[4000],D[4000],dp[13000]; cin>>n>>m; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) cin>>W[i]>>D[i]; for(i=1;i<=n;i++) for(j=m;j>=W[i];j--) { dp[j]=max(dp[j],dp[j-W[i]]+D[i]); //cout<<dp[j]<<endl; } cout<<dp[m]<<endl; return 0; }
相关文章推荐
- centreon installing
- Win7下用Eclipse远程连接Hadoop进行开发
- linux下mysql定时备份数据库
- 记录自己的开发历程
- leetcode:219 Contains Duplicate II-每日编程第三十五题
- 好的代码行
- DS-SDOJ-数据结构实验之排序三:bucket sort
- 算法设计题2.24-线性表-第2章-《数据结构习题集》-严蔚敏吴伟民版
- 程序猿如何提高自己的搜索技能
- android中使用Application传递数据
- 【原】SDWebImage源码阅读(三)
- NGUI制作字体
- python pip源
- 墙上时钟时间 ,用户cpu时间 ,系统cpu时间
- UVA10391 Compound Words
- 测试数据的生成
- 我是好人2
- 使用Tomcat发布网站
- XenDesktop5.6连接外部SQL Server数据库步骤
- Ubuntu14.04 使用visualBox安装虚拟机出现VT-x is disabled in the BIOS错误