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( ...
随机推荐
- CocosCreator - 向上传递事件(冒泡)
/** * 分发事件到事件流中. * this.node.dispatchEvent(new cc.Event.EventCustom("name",是否向上传递)) * ...
- 微信小程序调用用百度地图天气功能
#小程序之调用百度地图天气功能 本篇博客主要介绍小程序在百度地图中获取天气信息,如有不全请指出.下面先上效果图 主要内容 百度地图API的个人密钥,也就是AK 请求百度地图API接口数据 获取到的信息 ...
- Block循环引用问题(Objective-c)
造成循环引用的简单理解是:Block的拥有者在Block作用域内部又引用了自己,因此导致了Block的拥有者永远无法释放内存,就出现了循环引用的内存泄漏 示例代码 @interface ObjTest ...
- 了解facade设计模式
Facade模式 Facade模式要求一个子系统的外部与其内部的通信必须通过一个统一的Facade对象进行.Facade模式提供一个高层次的接口,使得子系统更易于使用. 就如同医院的接待员一样,Fac ...
- segger rtt 输出 log
调试 mcu 的时候,使用 jlink 的 rtt 可以方便的输出信息. 输出有两种模式, client 和 logger. client 必须依附其他程序,比如说 mdk 调试状态,或者 logge ...
- Working Plan 优先队列+贪心
题目链接:http://codeforces.com/gym/101987题目描述ICPC manager plans a new project which is to be carried out ...
- java的JDBC的事务学习
https://www.cnblogs.com/chy18883701161/p/11372089.html
- PHP - 验证码制作加验证
一,主页 index.php <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- 1. react 简书 项目初始化
1. 创建 react 项目 npx create-react-app my-app 2. src 目录下删除 除了 index.js index.css app.js 的文件 3. 引入 style ...
- 碎碎念(DP)
链接:https://ac.nowcoder.com/acm/contest/3006/F来源:牛客网 题目描述 在ACM比赛里,除了CE以外都是有效的提交.每一个提交都会有其评测的结果,或是AC,或 ...