来源: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. Jquery框架1.选择器|效果图|属性、文档操作

    1.JavaScript和jquery的对比 书写繁琐,代码量大 代码复杂 动画效果,很难实现.使用定时器 各种操作和处理 <!DOCTYPE html> <html lang=&q ...

  2. java作业第三次作业

    (一)作业总结 1.阅读下面程序,分析是否能编译通过?如果不能,说明原因.应该如何修改?程序的运行结果是什么? 为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来? class G ...

  3. Python6 - 函数总结

    一.函数的基本知识 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 1.1函数定义规则 ...

  4. Python 导入requests报错No module named requests

    刚开始是在Pycharm里面,发现就是不对,导入老提示No module named requests这个错误.后面发现是指向的python.exe路径不对,到setting里面设置换一下路径就好.

  5. FastAdmin 如何隐藏操作栏中的“删除”按钮“?

    刚刚在群里看到有人询问如何隐藏(删除)操作栏中的”删除”按钮,如下这个. 我也不会,在论坛上也没找到,但有找到一篇关于根据条件怎么隐藏按钮的问题,Karson 老大有提供的解决方法.我就照样画葫芦写了 ...

  6. C6000 CSL 函数说明

    转自:http://bbs.21ic.com/icview-741800-1-1.html 先来看一个例子 代码1 CSL_FINST(osdRegs->VIDWINMD, OSD_VIDWIN ...

  7. springboot mail+Thymeleaf模板

    compile 'org.springframework.boot:spring-boot-starter-thymeleaf' compile 'io.ratpack:ratpack-thymele ...

  8. iOS 技术篇:如何处理多个网络请求的先后(依赖)关系

    在开发过程中,不知你有没有碰到过在一个页面 用到了多个网络请求,而且根据业务需求,需要有次序的执行A B C 网络请求? 你可能会想到,我在A的请求成功回调里去处理B,在B的回调里去请求C,但你后来会 ...

  9. 每天刷Web面试题(前10天汇总)

    一.算法题部分   1. 如何获取浏览器URL中查询字符串中的参数? function getParamsWithUrl(url) {       var args = url.split('?'); ...

  10. Luogu2586 [ZJOI2008]杀蚂蚁 ---- 模拟

    Luogu2586 [ZJOI2008]杀蚂蚁 题意 还是一道大模拟 https://www.luogu.org/problemnew/show/P2586 大概就是炮塔大蚂蚁的故事 下载这个游戏ht ...