POJ3624 Charm Bracelet(典型01背包问题)
来源:互联网 发布:饥荒联机百科全书 mac 编辑:程序博客网 时间:2024/05/18 01:02
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 61 42 63 122 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 }
- POJ3624 Charm Bracelet(典型01背包问题)
- poj3624 Charm Bracelet DP 01背包问题
- POJ3624 Charm Bracelet(01背包问题)
- POJ3624 Charm Bracelet(01背包问题)
- POJ3624:Charm Bracelet【01背包】
- POJ3624 Charm Bracelet 【01背包】
- 【POJ3624】Charm Bracelet(01背包)
- POJ3624 - Charm Bracelet(01背包)
- poj3624 Charm Bracelet 01背包
- poj3624(Charm Bracelet + 赤裸01背包)
- 【POJ3624】Charm Bracelet 01背包裸题
- Charm Bracelet(poj3624)(01背包)
- POJ3624 Charm Bracelet (01背包)
- POJ3624 Charm Bracelet(01背包)
- poj3624 Charm Bracelet(01背包)
- POJ3624 Charm Bracelet 01背包裸题
- (poj3624)Charm Bracelet(01背包)
- POJ3624 Charm Bracelet(01背包~~简单DP~~)
- 编写linux驱动所用到的头文件
- 人工神经网络
- 每日打卡 2017.04.13 计算几何
- POJ1700 Crossing River(贪心算法训练)
- POJ1323 Game Prediction(贪心算法训练)
- POJ3624 Charm Bracelet(典型01背包问题)
- POJ1017 Packets(贪心算法训练)
- 嵌入式实验EX1:Kahn Process Networks and Synchronous Data Flows
- Opencv 中的结构体CvScalar
- nginx实现反向代理和负载均衡
- Java中的引用类型和垃圾回收
- Spring学习-25:Spring中的JDBC Template(JDBC模板):设置参数到属性文件
- 面试知识点6:MySQL中InnoDB的一级索引、二级索引
- An ffmpeg and SDL Tutorial 03