[转]POJ3624 Charm Bracelet(典型01背包问题)
来源:https://www.cnblogs.com/jinglecjy/p/5674796.html
题目链接:http://bailian.openjudge.cn/practice/4131/
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32897 Accepted: 14587
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
一、题目大意
有N个物品,分别有不同的重量Wi和价值Di,Bessie只能带走重量不超过M的物品,要是总价值最大,并输出总价值。
二、解题思路
典型的动态规划题目,用一个数组记录背包各个重量的最优解,不断地更新直到穷尽所有可能性。
状态更新公式:state[weight] = max{state[weight-W[i]]+D[i], state[weight]}
// 背包问题(动态规划)
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 3402
#define MAXM 12880
using namespace std; int main(){
int N, M, W[MAXN+], D[MAXN+], dp[MAXM+];
while(scanf("%d%d", &N, &M) != EOF){
for(int i=; i<N; i++){
scanf("%d%d", &W[i], &D[i]);
}
memset(dp, , sizeof(dp));
for(int i=; i<N; i++){
for(int left_w=M; left_w>=W[i]; left_w--){
dp[left_w] = max(dp[left_w-W[i]]+D[i], dp[left_w]);
}
}
printf("%d\n", dp[M]);
} return ;
}
[转]POJ3624 Charm Bracelet(典型01背包问题)的更多相关文章
- Charm Bracelet(01背包问题)
题目链接: https://vjudge.net/problem/POJ-3624 题目描述: Bessie has gone to the mall's jewelry store and spie ...
- Poj3624 Charm Bracelet (01背包)
题目链接:http://poj.org/problem?id=3624 Description Bessie has gone to the mall's jewelry store and spie ...
- POJ3624 Charm Bracelet 【01背包】
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22621 Accepted: 10157 ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- poj3624 Charm Bracelet(DP,01背包)
题目链接 http://poj.org/problem?id=3624 题意 有n个手镯,每个手镯有两个属性:重量W,需求因子D.还有一个背包,它能装下总重量不超过M的手镯.现在将一些镯子装入背包,求 ...
- POJ 3624 Charm Bracelet(01背包模板)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45191 Accepted: 19318 ...
- poj 3524 Charm Bracelet(01背包)
Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd ...
- 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 ...
- Charm Bracelet(01背包)
Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fil ...
随机推荐
- 如何将Oracle 当前日期加一天、一分钟
在Oralce中我发现有add_months函数,加天数N可以用如下方法实现,select sysdate+N from dual sysdate+1 加一天sysdate+1/24 加1小时sysd ...
- LINQ学习之旅 (四)
LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains 1.Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. ...
- Ajax和JSON完成二级菜单联动的功能
首先需要找好JSON的包哦: 链接:http://pan.baidu.com/s/1jH6gN46 密码:lbh1 1:首先创建一个前台页面,比如secondMenu.jsp,源码如下所示: < ...
- P2152 [SDOI2009]SuperGCD 未完成
辗转相减求a,b的gcd其实可以优化的: 1.若a为偶数,b为奇数:gcd(a,b)=gcd(a/2,b) 2.若a为奇数,b为偶数:gcd(a,b)=gcd(a,b/2) 3.若a,b都是偶数:gc ...
- \x 开头编码的数据解码成中文
在python里,直接decode('utf-8')即可 >>> "\xE5\x85\x84\xE5\xBC\x9F\xE9\x9A\xBE\xE5\xBD\x93 \xE ...
- Jenkins Pipline语法
引用自:http://baijiahao.baidu.com/s?id=1582812185263227836&wfr=spider&for=pc 引用自:https://www.cn ...
- Nightmare HDU1072
非常标准的BFS 第一次写错了很多 1.到达4时设置为墙就好了 避免了死循环 2.不用开d数组 在结构体里面就行了 3.结构体初始化函数的写法: Node(int x=0,int y=0,int ...
- Win10 下 hadoop3.0.0 单机部署
前言 因近期要做 hadoop 有关的项目,需配置 hadoop 环境,简单起见就准备进行单机部署,方便开发调试.顺便记录下采坑步骤,方便碰到同样问题的朋友们. 安装步骤 一.下载 hadoop-XX ...
- IdentityServer4-EF动态配置Client和对Claims授权(二)
本节介绍Client的ClientCredentials客户端模式,先看下画的草图: 一.在Server上添加动态新增Client的API 接口. 为了方便测试,在Server服务端中先添加swagg ...
- 说出ArrayList,Vector, LinkedList的存储性能和特性
ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插 ...