[转]POJ3624 Charm Bracelet(典型01背包问题)

简介: 来源:https://www.cnblogs.com/jinglecjy/p/5674796.html题目链接:http://bailian.openjudge.cn/practice/4131/Time Limit: 1000MS          Memory Limit: 65536K  ...

来源: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]}

 

 1 // 背包问题(动态规划)
 2 #include <iostream> 
 3 #include <cstdio>
 4 #include <cstring>
 5 #define MAXN 3402
 6 #define MAXM 12880
 7 using namespace std;
 8 
 9 int main(){
10     int N, M, W[MAXN+5], D[MAXN+5], dp[MAXM+5];
11     while(scanf("%d%d", &N, &M) != EOF){
12         for(int i=0; i<N; i++){
13             scanf("%d%d", &W[i], &D[i]);
14         }
15         memset(dp, 0, sizeof(dp));
16         for(int i=0; i<N; i++){
17             for(int left_w=M; left_w>=W[i]; left_w--){
18                 dp[left_w] = max(dp[left_w-W[i]]+D[i], dp[left_w]);
19             }
20         }
21         printf("%d\n", dp[M]);
22     }
23     
24     return 0;
25 }

 

相关文章
|
6月前
|
存储
poj 3254 Corn Fields (状态压缩dp)
状态压缩dp其实就是用二进制来表示所有的状态,比如这题, 我们在某一行可以这样取0 1 0 1 1 0 1,用1代表取了,0代表没取,因为这点,它的数据量也限制在20以内,所有看到这样数据量的题目可以先考虑一下状态压缩dp。对于有多行的数据,所有状态的总数必然很庞大,而且不用特殊的方法想要存储这些状态是不太现实的。既然每个点只有这两种情况,我们可以用二进制的一位来表示,0 1 0 1 1 0 1就可以表示为二进制0101101也就是十进制的45,如果我们想要枚举6个点的所有状态,我们只需要从0到2^6取其二进制就可以了,并不会重复或是多余。
18 0
|
6月前
poj 3624 Charm Bracelet(简单01背包)
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.
19 0
POJ-3624,Charm Bracelet(01背包)
POJ-3624,Charm Bracelet(01背包)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
HDU-2602,Bone Collector(01背包)
HDU-2602,Bone Collector(01背包)
|
人工智能 BI
[UVA 1599] Ideal Path | 细节最短路
Description New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci .
175 0
Mad Scientist (纯模拟题)
Mad Scientist 题目描述 Farmer John’s cousin Ben happens to be a mad scientist. Normally, this creates a good bit of friction at family gatherings, but it can occasionally be helpful, especially when Farmer John finds himself facing unique and unusual problems with his cows.
118 0
|
测试技术
Knight Moves(骑士移动) (bfs) POJ-2243
题目:Knight Moves (骑士移动)