Charm Bracelet

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 the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (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 and Di

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
首先我们要明白i,j,dp[i][j]都分别代表着什么
i:表示从1~i个物品里取
j:表示此时的最大容积
dp[i][j]:表示在1~i个物品里选取,每个物品只选择一次(限于01背包),且不超过j的容积,问所能得到的最大价值
得出一个转移方程f[i][j] = max(f[i][j], f[i - 1][j - c[i]] + w[i])
即在选取这个物品和不选这个物品中选择一个最大的
最后我们要输出的就是dp[n][m]表示在n个物品中,容积不超出m的最大价值
 #include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[][];
int w[],c[];
int main()
{
int n,m;
scanf ("%d%d",&n,&m);
for (int i = ;i<= n;i++)
{
scanf ("%d%d",&w[i],&c[i]);
}
for (int i = ;i <= n;i++)
{
for (int j = ;j <= m;j++)
{
dp[i][j] = dp[i-][j];
if (j-w[i]>=)
{
dp[i][j]=max(dp[i-][j],dp[i-][j-w[i]]+c[i]); } }
// cout<<dp[i][m]<<endl;
}
cout<<dp[n][m];
return ;
}
先考虑上面讲的基本思路如何实现,肯定是有一个主循环i=1..N,每次算出来二维数组f[0..V]的所有值。那么,如果只用一个数组f[0..V],能不能保证第i次循环结束后f[v]中表示的就是我们定义的状态f[v]呢?f[v]是由f[v]和f[v-c]两个子问题递推而来,能否保证在推f[v]时(也即在第i次主循环中推f[v]时)能够得到f[v]和f[v-c]的值呢?事实上,这要求在每次主循环中我们以v=V..0的顺序推f[v],这样才能保证推f[v]时f[v-c]保存的是状态f[v-c]的值。伪代码如下:
for i=1..N
for v=V..0
f[v]=max{f[v],f[c]+w};
其中的f[v]=max{f[v],f[c]}一句恰就相当于我们的转移方程f[v]=max{f[v],f[c]},因为现在的f[c]就相当于原来的f[c]。如果将v的循环顺序从上面的逆序改成顺序的话,那么则成了f[v]由f[c]推知,与本题意不符
 #include<iostream>
using namespace std;
int f[];
int main()
{
int n,v;
int w[], c[];
cin >> n >> v;
for(int i = ; i <= n; ++i)
cin >> w[i] >>c[i];
for(int i = ; i <= n; ++i){
for (int j=v ;j>=w[i] ; j--){
f[j] = max(f[j], f[j - w[i]] + c[i]);
}
}
cout << f[v]<<endl;
return ;
}
												

dp--01背包--Charm Bracelet的更多相关文章

  1. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  2. USACO Money Systems Dp 01背包

    一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...

  3. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  4. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  5. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  6. UVA.10130 SuperSale (DP 01背包)

    UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...

  7. poj 2923 状压dp+01背包

    好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...

  8. DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)

    题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp ...

  9. hihoCoder#1055 : 刷油漆 (树形DP+01背包)

    题目大意:给一棵带点权的树,现在要从根节点开始选出m个连通的节点,使总权值最大. 题目分析:定义状态dp(u,m)表示在以u为根的子树从根节点开始选出m个点连通的最大总权值,则dp(u,m)=max( ...

随机推荐

  1. iptable实现端口转发

    利用iptables的规则来实现端口转发: 第一步需要将内核参数的net.ipv4.ip_forward=1 场景一:实现本地端口转发 本地端口转发实在PREROUTING链中将端口做NAT转换: # ...

  2. Vulkan 之 Debugging

    1.可以用validation layers 进行验证: 2.Snapdragon Profiler 使用说明

  3. Emacs服务器模式以及emacsclient配置

    Emacs有很多强大的插件,但是插件安装多了会导致启动速度很慢.为了解决这个问题,一个方法是使用emacs提供的server模式. 基本用法[1] 启动emacs server: $ emacs -- ...

  4. 104-PHP定义并实例化类

    <?php class ren{ //定义人类 } class mao{ //定义猫类 } echo '实例化一个人类:'; var_dump(new ren()); //实例化人类 echo ...

  5. 数据结构必做题参考:实验一T1-20,实验2 T1

    实验一T1-10 #include <bits/stdc++.h> using namespace std; ; struct Book { string isbn; string nam ...

  6. tensorflow-cnnn-mnist

    #coding=utf-8import tensorflow as tfimport numpy as npimport matplotlib .pyplot as pltfrom tensorflo ...

  7. 通过SQL语句操作Sqlite数据库

    一.数据库的创建 数据库版本为1 //Ctrl+Shift+U:大写 public static final String DATABASE_NAME ="zzw.db"; pub ...

  8. Mac Go 环境变量配置

    GOPATH 是工作目录,就是你打代码,代码的存放目录 GOROOT 是Go的安装目录,我下载的是免安装版的 现在的Go环境变量就是设置成这个样子, 终于Bee不会报错了!!!

  9. oracle数据删除恢复

    insert into 表名 select * from 表名 as of timestamp to_Date('2017-07-20 10:00:00', 'yyyy-mm-dd hh24:mi:s ...

  10. VS Code 配置vue开发环境

    一.插件 网上搜索vscode插件的文章,动辄十几个,其实根本用不了那么多,很多插件的作用还有重叠,电脑性能还被白白浪费.这里精简为主,每一个插件都发挥它最大的作用,并尽量说明它们的作用 Vetur ...