来源: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背包问题)的更多相关文章

  1. Charm Bracelet(01背包问题)

    题目链接: https://vjudge.net/problem/POJ-3624 题目描述: Bessie has gone to the mall's jewelry store and spie ...

  2. Poj3624 Charm Bracelet (01背包)

    题目链接:http://poj.org/problem?id=3624 Description Bessie has gone to the mall's jewelry store and spie ...

  3. POJ3624 Charm Bracelet 【01背包】

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22621   Accepted: 10157 ...

  4. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  5. poj3624 Charm Bracelet(DP,01背包)

    题目链接 http://poj.org/problem?id=3624 题意 有n个手镯,每个手镯有两个属性:重量W,需求因子D.还有一个背包,它能装下总重量不超过M的手镯.现在将一些镯子装入背包,求 ...

  6. POJ 3624 Charm Bracelet(01背包模板)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45191   Accepted: 19318 ...

  7. poj 3524 Charm Bracelet(01背包)

    Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd ...

  8. 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 ...

  9. Charm Bracelet(01背包)

    Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fil ...

随机推荐

  1. for循环输出数组中的分数

    示例 var scores = [24, 32, 17]; // A数组 var arrayLength = scores.length;// 数组的长度 //当i<arrayLength时,可 ...

  2. python学习之基本数据类型

    python的基本数据类型有数字.字符串.列表.字典.元祖.布尔值 一.数字 1.1.字符转换为数字 实例: a=" b=int(a) print(b+) 运行结果: 可以用type查看数据 ...

  3. 020 $.each的使用

    在工作中见到这个知识点,不是特别懂,就查了查资料,顺便整理一下 1.定义与用法 each() 方法规定为每个匹配元素规定运行的函数. 提示:返回 false 可用于及早停止循环. 语法 $(selec ...

  4. 汇编之 eax, ebx, ecx, edx, esi, edi, ebp, esp??

    一般寄存器:AX.BX.CX.DXAX:累积暂存器,BX:基底暂存器,CX:计数暂存器,DX:资料暂存器 索引暂存器:SI.DISI:来源索引暂存器,DI:目的索引暂存器 堆叠.基底暂存器:SP.BP ...

  5. Java 处理 iphone拍照后 图片EXIF属性翻转90度的方法

    http://blog.csdn.net/z69183787/article/details/50320821 Java获取照片EXIF信息 http://blog.csdn.net/ghsau/ar ...

  6. AngularJS移动端页面input无法输入

    用angularJS写手机页面,有时候会发现input输入框点击了却不能输入,或者长按才能输入,可能是因为input绑定了ng-click导致,可去掉ng-click,将ng-click绑定的方法改用 ...

  7. python2和3在处理字符串上的区别

    python2和3在处理字符串上的区别   python2和python3对于字符串的处理有很大的区别 熟悉了python2的写法用python3时真的会遇到很多问题啊…… 区别 python2中有一 ...

  8. 微信小程序:一起玩连线,一个算法来搞定

    微信小程序:一起玩连线 游戏玩法 将相同颜色的结点连接在一起,连线之间不能交叉. 算法思想 转换为多个源点到达对应终点的路径问题,且路径之间不相交.按照dfs方式寻找两个结点路径,一条路径探索完之后, ...

  9. Spring框架学习08——自动代理方式实现AOP

    在传统的基于代理类的AOP实现中,每个代理都是通过ProxyFactoryBean织入切面代理,在实际开发中,非常多的Bean每个都配置ProxyFactoryBean开发维护量巨大.解决方案:自动创 ...

  10. 洛谷P2982 [USACO10FEB]慢下来Slowing down(线段树 DFS序 区间增减 单点查询)

    To 洛谷.2982 慢下来Slowing down 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows con ...