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 ≤ 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

01背包主要关系:
if (dp[j]<dp[j-w[i]]+d[i])
            dp[j]=dp[j-w[i]]+d[i];

对于题给的例子:

             1           2        3          2

dp[1]       4           4         4        4   

dp[2]       4           6         6        7

dp[3]       4           10       12        12

dp[4]       4           10       16        13

dp[5]       4           10       18        19    

dp[6]       4           10       22         23   

dp[j]表示容量为j时能放的最多东西。从1到n个物品一个一个的放和比较。
 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int main()
 5 {
 6     int w[3500],d[3500],n,m,i,j,dp[13500];
 7     while (~scanf("%d%d",&n,&m))
 8     {
 9         memset(dp,0,sizeof(dp));
10         for (i=1;i<=n;i++) scanf("%d%d",&w[i],&d[i]);
11         for (i=1;i<=n;i++)
12         {
13             for (j=m;j>=w[i];j--)
14             if (dp[j]<dp[j-w[i]]+d[i])
15             dp[j]=dp[j-w[i]]+d[i];
16         }
17         printf("%d\n",dp[m]);
18     }
19 }
时间: 2024-10-13 02:37:05

POJ 3624 Charm Bracelet (01)(背包入门)的相关文章

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 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背包)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26078   Accepted: 11726 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背包基础)

题意: 即是给你一个容量M的包,有N件物品,每件物品有分别对应的 价值value 以及 重量weight .然后在不超过该背包容量的情况下能得到的最大价值为多少? 思路: 由于这是最基础的问题,所以就记录当对 01背包状态转移方程式的 理解. 对于动态规划来说,首先要知道我们要确定哪些状态量.然后再基于这些状态量进行状态转移得到我们最后希望得到的答案. 比如对于序列求最值来说我们习惯记录最后位取的是谁(即末位j),那么同理我们也可以记录我们当前选的是哪一种 物品.同时容量在状态中是必不可少的,所

[再做01背包] POJ 3624 Charm Bracelet

接触动态规划的第一题是数塔问题,第二题就是01背包问题了. 当时看的懵懵懂懂,回过头来再看这道题还是非常简单的了. 用 dp[i][j] 表示取前i种物品,使它们总体积不超过j的最优取法取得的价值总和状态转移方程:dp[i][j] = max(dp[i-1][j],dp[i-1][j-cost[i]]+weight[i]) 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstri

POJ 3624 Charm Bracelet 背包题解

最简单的背包问题了,本题应该除了背包就一个考点了:不能开二维数组.我没开过二维,不过看数据是不可以的.太大了. 做法有两种改进省内存DP: 1 所谓的滚动数组 2 逆向填表 很久没做背包DP,突然觉得这种背包问题很简单了. 下面给出两种解法: 1 calBag()是滚动数组 2 calBag2()是逆向填表 #pragma once #include <stdio.h> #include <stdlib.h> #include <vector> using namesp

POJ 3624 Charm Bracelet

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 weightWi (1 ≤ 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背包】

解题思路:直接套公式就能做的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--) //即最开始给定包的总容量(此时包是空的),循

2、Charm Bracelet( poj 3624)简单0-1背包

题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <stack> #include <queue> #include <math.h> #