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

Source

解题思路:

  又是一道01背包的模板题,其实只要把握清楚思想就好了,用一维来搞,用二维会爆掉。。。

代码:

 1 # include<cstdio>
 2 # include<iostream>
 3 # include<cstring>
 4
 5 using namespace std;
 6
 7 # define MAX 3555
 8
 9 int a[MAX],b[MAX];
10 int dp[MAX*MAX];
11
12 int main(void)
13 {
14     int n,m;
15     while ( scanf("%d%d",&n,&m)!=EOF )
16     {
17         memset(dp,0,sizeof(dp));
18         for ( int i = 0;i < n;i++ )
19         {
20             scanf("%d%d",&a[i],&b[i]);
21         }
22         for ( int i = 0;i < n;i++ )
23         {
24             for ( int j = m;j >= a[i];j-- )
25             {
26                 dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
27             }
28         }
29         printf("%d\n",dp[m]);
30
31     }
32
33     return 0;
34 }
时间: 2024-10-12 19:43:38

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背包基础)

题意: 即是给你一个容量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)(背包入门)

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 

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