dp--01背包--Charm Bracelet
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
#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 ;
}
#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的更多相关文章
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- USACO Money Systems Dp 01背包
一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- poj 2923 状压dp+01背包
好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...
- DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)
题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp ...
- hihoCoder#1055 : 刷油漆 (树形DP+01背包)
题目大意:给一棵带点权的树,现在要从根节点开始选出m个连通的节点,使总权值最大. 题目分析:定义状态dp(u,m)表示在以u为根的子树从根节点开始选出m个点连通的最大总权值,则dp(u,m)=max( ...
随机推荐
- Oracle SQL 异常处理
今天学了异常处理 有预定义异常 与 用户自定义异常 还有 raise_application_error()函数raise_application_error() 只能把异常抛出而不能处理异常. 预定 ...
- 十四、JavaScript之不同类型变量相加
一.代码如下 二.效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" conten ...
- Linux 下 zip 文件解压乱码如何解决
作者:Latm Ake链接:https://www.zhihu.com/question/20523036/answer/35225920来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...
- centos7下使用yum安装ifconfig工具
步骤1:搜索安装包 步骤2:使用yum安装 至此,ifconfig工具安装完毕,希望对你有帮助~
- border-radius 在 浏览器开发者工具移动端里是有效的,在真机是无效的。
border-radius 在 浏览器开发者工具移动端里是有效的,在真机是无效的,怎么解决? 答案是 border-radius:20px !important 加上!important 就好了.
- opencv+python实时人脸检测、磨皮
import numpy as np import cv2 cap = cv2.VideoCapture(0) face_cascade = cv2.CascadeClassifier("d ...
- Python MySQL Select
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- MongoDB七-运维技术
复制来自:http://www.cnblogs.com/huangxincheng/archive/2012/03/08/2384571.html 这一篇我们以管理员的视角来看mongodb,作为一名 ...
- CodeForces - 401C Team(简单构造)
题意:要求构造一个字符串,要求不能有连续的两个0在一起,也不能有连续的三个1在一起. 分析: 1.假设有4个0,最多能构造的长度为11011011011011,即10个1,因此若m > (n + ...
- POJ 2182&& POJ 2828:Lost Cows 从后往前 线段树
Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10544 Accepted: 6754 Descri ...