来源: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. Mysql在master上查看有哪些slave

    mysql> select * from information_schema.processlist as p where p.command = 'Binlog Dump'; 或 mysql ...

  2. 迪米特法则(Law of Demeter, LoD)

    一个软件实体应当尽可能少地与其他实体发生相互作用 未完待续

  3. php输出json的内容

    $json = '{"foo": 12345}'; $obj = json_decode($json); print $obj->{'foo'}; // 12345

  4. 【Java】 剑指offer(32) 从上往下打印二叉树

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 (一)从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺 ...

  5. Linux 之 AT&T汇编语言 mov、add、sub指令、数据段

    mov指令的几种形式: mov 寄存器. 数据 mov ax,8888 mov 寄存器. 寄存器 mov bx,ax mov 寄存器. 内存单元 mov ax,[0] mov 内存单元.寄存器 mov ...

  6. HDU-2066-一个人的旅行 【Dijkstra】

    <题目链接> 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历, ...

  7. hdu 1166 敌兵布阵【线段树】(求给定区间和)

    题目链接:https://vjudge.net/contest/182746#problem/B       敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)   ...

  8. 【nodeJS爬虫】前端爬虫系列

    写这篇 blog 其实一开始我是拒绝的,因为爬虫爬的就是cnblog博客园.搞不好编辑看到了就把我的账号给封了:). 言归正传,前端同学可能向来对爬虫不是很感冒,觉得爬虫需要用偏后端的语言,诸如 ph ...

  9. Javascript日常编码中的一些常见问题

    一.尽量少用全局变量   这是一个疑问最少,同时流传最 广的一条.Javascript使用函数管理作用域,全局变量最大的问题在于同名变量冲突.这种隐患产生比较直接的两个原因就是Javascript语言 ...

  10. 生产环境中tomcat的配置

    生产环境中要以daemon方式运行tomcat 通常在开发环境中,我们使用$CATALINA_HOME/bin/startup.sh来启动tomcat, 使用$CATALINA_HOME/bin/sh ...