[转]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 ...
随机推荐
- 2017-2018-2 20155309 南皓芯 Exp3 免杀原理与实践
报告内容 2.1.基础问题回答 (1)杀软是如何检测出恶意代码的 ? 1:基于特征码 一段特征码就是一段或多段数据.(如果一个可执行文件(或其他运行的库.脚本等)包含这样的数据则被认为是恶意代码) 杀 ...
- GDIPlus非典型误用一例
// ** 初始化GDI+ Gdiplus::GdiplusStartupInput gdiplusStartupInput; // ** 该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标 ...
- poj 1256 按一定顺序输出全排列(next_permutation)
Sample Input 3aAbabcacbaSample Output AabAbaaAbabAbAabaAabcacbbacbcacabcbaaabcaacbabacabcaacabacbaba ...
- [转] react-router4 实现按需加载
按需加载的背景 https://juejin.im/post/58f9717e44d9040069d06cd6?utm_source=tuicool&utm_medium=referral R ...
- day8--socketserver
socketserver分类: 1.TCP协议 class socketserver.TCPServer(server_address,RequestHandlerClass,bind_and_act ...
- Codeforces 679C Bear and Square Grid
Bear and Square Grid 枚举k * k 的位置, 然后接上它周围白色连通块的数量, 再统计完全在k * k范围里的连通块, 这个只要某个连通块全部的方格 在k * k里面就好, 并且 ...
- 关于BeanUtils.copyProperties() 用法及区别
这两个类在不同的包下面,而这两个类的copyProperties()方法里面传递的参数赋值是相反的. 例如:a,b为对象BeanUtils.copyProperties(a, b); BeanUtil ...
- 兼容ie10以下版本的placeholder属性
<script src="${ctx }/js/jquery.placeholder.js" type="text/javascript">< ...
- HDU 1025 城市供应 【LIS】
题目链接:https://vjudge.net/contest/228455#problem/A 题目大意: 有2n个城市,其中有n个富有的城市,n个贫穷的城市,其中富有的城市只在一种资源富有,且富有 ...
- muduo网络库架构总结
目录 muduo网络库简介 muduo网络库模块组成 Recator反应器 EventLoop的两个组件 TimerQueue定时器 Eventfd Connector和Acceptor连接器和监听器 ...