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

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 } 

时间: 2024-12-25 20:22:12

POJ3624 Charm Bracelet(典型01背包问题)的相关文章

POJ3624 Charm Bracelet 【01背包】

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22621   Accepted: 10157 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 fro

Charm Bracelet(01背包)

Problem 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 

POJ 3624 Charm Bracelet(01背包模板)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45191   Accepted: 19318 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 fro

POJ 3624 Charm Bracelet (01)(背包入门)

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 

poj3624 Charm Bracelet

http://poj.org/problem?id=3624 题目大意:贝茜去了商场的珠宝店,发现了一个迷人的手镯.当然,她想装最好的魅力(N(1≤N≤3402)可用的魅力).每个魅力提供的列表中都有一个重量Wi(1≤Wi≤400),一个"愿望"因素Di(1≤Di≤100),最多可以使用一次.贝茜只能支持一个重量不超过M的魅力手镯. *第1行:两个空格分隔的整数:N和M. *第2 -N+1行:第i+1行描述了具有两个空间分离整数的魅力i: Wi和Di.考虑到重量限制作为一种约束,并列举

poj3624 Charm Bracelet(0-1背包 滚动数组)

题目链接:https://vjudge.net/problem/POJ-3624 题意:有N个物品,分别有不同的重量Wi和价值Di,Bessie只能带走重量不超过M的物品,要是总价值最大,并输出总价值. 一开始使用正常的dp然后显示超内存,按下面代码也超内存(dp数组太大了)但这种方法可以学习一下 #include <iostream> #include <cstring> using namespace std; int n,m; //int w[3403],d[12881];

POJ 3624 Charm Bracelet【01背包】

解题思路:直接套公式就能做的01背包, for(i=1;i<=n;i++) { for(v=w[i];v<=m;v++) f[i,v]=max(f[i,v],f[i-1,v-w[i]]+d[i]);//只想明白了可以用一维数组来存放包的价值,因为我们需要的只是包的最大价值,不用记录是第几个包的时候,有最大价值,然后v从w[i]到包的总容量循环不明白. } for(i=1;i<=n;i++) { for(v=m;v>=c[i];v--) //即最开始给定包的总容量(此时包是空的),循

bzoj1625 / P2871 [USACO07DEC]手链Charm Bracelet

P2871 [USACO07DEC]手链Charm Bracelet 裸01背包. 看到自己1年半前写的30分code.......菜的真实(捂脸) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define re register 5 using namespace std; 6 int max(int a,int b){return a>b?a:b;} 7 int n,m,f[12

0-1背包问题,附上例题(POJ - 3624 Charm Bracelet)

0-1背包问题的题目样式 有 N 件物品和一个容量为 M 的背包.放入第 i 件物品耗费的费用是 Wi,得到的价值是 Vi.求解将哪些物品装入背包可使价值总和最大. 0-1背包问题关键在于该物品放或不放,即在当前容量为M的的情况下,选择不选择该物品,那么就有一个转移方程 for(i=0  -  N) for(j=0  -  M) dp[i][j] = max(dp[i-1][j],dp[i-1][j+w[i]]+v[i]); 当前物品为i,当前的背包容量为j,如果不选当前该物品,则选取dp[i-