POJ 3624 Charm Bracelet(01背包模板)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 45191 | Accepted: 19318 |
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 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
Source
问题分析:
N 个物品每个物品有价值v[i],重量w[i], 给定背包最大承重M,求背包能够装载的最大价值。每个物品只有放入背包和不放入背包两种选择。
这是典型的0-1背包问题。
代码的时间上限是O(nm), 对于每个物品i, 它所要遍历的整数区间都是[ci, m]
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
int dp[];
int w[];
int v[];
int main()
{
int n,m;
cin>>n>>m;
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
cin>>w[i]>>v[i];
}
for(int i=;i<=n;i++)
{
for(int j=m;j>=w[i];j--)//要倒着推过来,因为dp【j】是滚动数组
{ dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
}
}
cout<<dp[m];
return ;
}
POJ 3624 Charm Bracelet(01背包模板)的更多相关文章
- POJ 3624 Charm Bracelet(01背包模板题)
题目链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52318 Accepted: 21912 Descriptio ...
- POJ 3624 Charm Bracelet(01背包裸题)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38909 Accepted: 16862 ...
- POJ 3624 Charm Bracelet(01背包)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34532 Accepted: 15301 ...
- POJ 3624 Charm Bracelet 0-1背包
传送门:http://poj.org/problem?id=3624 题目大意:XXX去珠宝店,她需要N件首饰,能带的首饰总重量不超过M,要求不超过M的情况下,使首饰的魔力值(D)最大. 0-1背包入 ...
- poj 3624 Charm Bracelet 01背包问题
题目链接:poj 3624 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放. 用子问题定义状态:即F [i, v]表示前i件物品恰放入一个容量为v 的背包可以 ...
- 洛谷 P2871 [USACO07DEC]手链Charm Bracelet && 01背包模板
题目传送门 解题思路: 一维解01背包,突然发现博客里没有01背包的板子,补上 AC代码: #include<cstdio> #include<iostream> using ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- POJ 3624 Charm Bracelet (01背包)
题目链接:http://poj.org/problem?id=3624 Bessie has gone to the mall's jewelry store and spies a charm br ...
- POJ 3624 Charm Bracelet(0-1背包模板)
http://poj.org/problem?id=3624 题意:给出物品的重量和价值,在重量一定的情况下价值尽可能的大. 思路:经典0-1背包.直接套用模板. #include<iostre ...
随机推荐
- SpringBoot Mybatis 入门
Mybatis for Java API官方文档:http://www.mybatis.org/mybatis-3/zh/java-api.html Mybatis语法介绍 @Select 查询,所有 ...
- flume 使用遇到问题及解决
1. ../flume/fchannel/spool/data/ 目录下发生缓存文件积压 可能原因:同一时间同一客户端下向两个监控目录mv文件:或同一时间多个客户端向服务端上传文件 2.清空../fl ...
- Oracle、Mysql、SqlServer创建表和给表和字段加注释
一.Oracle --创建表 create table test ( id varchar2(200) primary key not null, sort number, ...
- 生成 (web): 找不到目标 .NET Framework 版本的引用程序集;请确保已安装这些程序集或选择有效的目标版本。
刚刚还好好的,不知道修改什么了,突然出现如下错误: Default.aspx(36): 生成 (web): 找不到目标 .NET Framework 版本的引用程序集:请确保已安装这些程序集或选择有效 ...
- jQuery颜色面板插件
/** * jQuery颜色面板插件 * * 使用方法:input框的id默认为inputObj,button框的id默认为btnObj,也可以自定义aaa,bbb * 1.初始化颜色面板:$.col ...
- vue组件 Prop传递数据
组件实例的作用域是孤立的.这意味着不能(也不应该)在子组件的模板内直接引用父组件的数据.要让子组件使用父组件的数据,我们需要通过子组件的props选项. prop 是单向绑定的:当父组件的属性变化时, ...
- struts2学习(4)
Struts2拦截器概述 1 Struts2是框架,封装了很多功能,struts2里面封装的概念都是在拦截器里面 2 Struts2里面封装了很多的概念,有很多拦截器,不是每次这些拦截器都执行,每次执 ...
- review23
文件的创建与删除 当使用File类创建一个文件对象后,例如 File file = new File("C:\\myletter", "letter.txt") ...
- TCP/IP 详解笔记
最早的 TCP 协议文档是 RFC793. TCP 提供一种面向连接的.可靠的字节流服务. 面向连接容易理解,那么什么是字节流服务呢? 答:两个应用程序通过 TCP 连接交换 8 bit 字节构成的字 ...
- Android中字体颜色的设置
1.在Android中经常看到设置的颜色为八位的十六进制的颜色值,例如: 1 2 3 public static final class color { public static final ...