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( ...
随机推荐
- iptable实现端口转发
利用iptables的规则来实现端口转发: 第一步需要将内核参数的net.ipv4.ip_forward=1 场景一:实现本地端口转发 本地端口转发实在PREROUTING链中将端口做NAT转换: # ...
- Vulkan 之 Debugging
1.可以用validation layers 进行验证: 2.Snapdragon Profiler 使用说明
- Emacs服务器模式以及emacsclient配置
Emacs有很多强大的插件,但是插件安装多了会导致启动速度很慢.为了解决这个问题,一个方法是使用emacs提供的server模式. 基本用法[1] 启动emacs server: $ emacs -- ...
- 104-PHP定义并实例化类
<?php class ren{ //定义人类 } class mao{ //定义猫类 } echo '实例化一个人类:'; var_dump(new ren()); //实例化人类 echo ...
- 数据结构必做题参考:实验一T1-20,实验2 T1
实验一T1-10 #include <bits/stdc++.h> using namespace std; ; struct Book { string isbn; string nam ...
- tensorflow-cnnn-mnist
#coding=utf-8import tensorflow as tfimport numpy as npimport matplotlib .pyplot as pltfrom tensorflo ...
- 通过SQL语句操作Sqlite数据库
一.数据库的创建 数据库版本为1 //Ctrl+Shift+U:大写 public static final String DATABASE_NAME ="zzw.db"; pub ...
- Mac Go 环境变量配置
GOPATH 是工作目录,就是你打代码,代码的存放目录 GOROOT 是Go的安装目录,我下载的是免安装版的 现在的Go环境变量就是设置成这个样子, 终于Bee不会报错了!!!
- oracle数据删除恢复
insert into 表名 select * from 表名 as of timestamp to_Date('2017-07-20 10:00:00', 'yyyy-mm-dd hh24:mi:s ...
- VS Code 配置vue开发环境
一.插件 网上搜索vscode插件的文章,动辄十几个,其实根本用不了那么多,很多插件的作用还有重叠,电脑性能还被白白浪费.这里精简为主,每一个插件都发挥它最大的作用,并尽量说明它们的作用 Vetur ...