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 ...
随机推荐
- 【转载】关于C++中cin的几点说明性总结
转载地址:http://www.07net01.com/program/289153.html 学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结: 1.cin 2.cin.get() 3.ci ...
- 用nc做网络压力测试
测试结果: 1.数据的收发正常,没有出现丢包: 2.平均数据接发速率为:112MB/S,基本用完的千兆带宽. 测试方法: 1.通过FTP拷贝3.6G ...
- 【P1274】魔术数字游戏(搜索+剪枝+模拟)
做完了这个题的我一口老血喷在屏幕上... 这个题难度不高(~~胡扯~~),就是爆搜就可以了,然而..判断条件灰常多,剪枝也就非常多..然而,这些判断条件又不得不必须满足,所以也就十分容易错... 说一 ...
- SQL server 2008 T-sql 总结
数据库的实现 1.添加数据:insert [into] 表名 (字段1,字段2,···) values (值1,值2,····) 其中,into可选. 2.修改数据:update 表名 set ...
- 关于UML方法学图中类之间的关系:依赖,泛化,关联
类与类图 1) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 2) 在系统中,每个类具有一定的职责,职责指的是类所担任的任务,即类要完成什 ...
- ZC_问题
1. [02][40:15]主键 还需要另外创建 序列? 一直到 [03][31:25]都已经成功的insert了两条数据了,也没见 手动创建 序列... 2. 貌似记得 有地方给 hibernate ...
- linux---mysql忘记密码
二.忘记原来的myql的root的密码: 首先,你必须要有操作系统的root权限了.要是连系统的root权限都没有的话,先考虑root系统再走下面的步骤.类似于安全模式登录系统,有人建议说是pkill ...
- UI(UGUI)框架(二)-------------UIManager单例模式与开发BasePanel面板基类/UIManage统一管理UI面板的实例化/开发字典扩展类
UIManage单实例: /// 单例模式的核心 /// 1,定义一个静态的对象 在外界访问 在内部构造 /// 2,构造方法私有化 private static UIManager _instanc ...
- ural 2023 Donald is a postman(水)
2023. Donald is a postman Time limit: 1.0 secondMemory limit: 64 MB Donald Duck works as a postman f ...
- oralce 索引(1)
本文来自网上整理 来自以下博客内容 http://www.360doc.com/content/13/0712/11/13136648_299364992.shtml; http://www.cnbl ...