D - Charm Bracelet 背包问题

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 3624

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
 1 Home    Problems    Status    Contest    [xiong_tao]    Logout
 2 xiong_tao ‘s source code for D
 3 Memory: 1952 KB         Time: 313 MS
 4 Language: G++         Result: Accepted
 5
 6 1
 7 2
 8 3
 9 4
10 5
11 6
12 7
13 8
14 9
15 10
16 11
17 12
18 13
19 14
20 15
21 16
22 17
23 18
24 19
25 20
26 21
27
28 #include<cstdio>
29 #include<string.h>
30 using namespace std;
31 int dp[400000],w[400000],d[4000];
32 int n,m;
33 int main()
34 {
35     int i,j;
36     while(scanf("%d%d",&n,&m)!=EOF)
37     {
38         for(i=0;i<n;i++)
39             scanf("%d%d",&w[i],&d[i]);
40         memset(dp,0,sizeof(dp));
41         for(i=0;i<n;i++)
42             for(j=m;j>=0;j--)
43                if(j>=w[i])
44                dp[j]=dp[j]>dp[j-w[i]]+d[i]?dp[j]:dp[j-w[i]]+d[i];
45         printf("%d\n",dp[m]);
46     }
47     return 0;
48 }
49
50 FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
51 All Copyright Reserved ©2010-2012 HUST ACM/ICPC TEAM
52 Anything about the OJ, please ask in the forum, or contact author:Isun
53 Server Time: 2014-07-28 18:43:07

D - Charm Bracelet 背包问题,布布扣,bubuko.com

时间: 2025-01-11 06:20:03

D - Charm Bracelet 背包问题的相关文章

POJ 3624 Charm Bracelet 背包问题的解决方案

最简单的背包问题,标题应该是除了背包测试中心:您无法打开二维数组.我还没有开的二维.光看数据是不可能的. 太大. 有两种方法来提高全省内存DP: 1 所谓卷的阵列 2 反向表 久没做背包DP,突然认为这样的背包问题非常easy了. 以下给出两种解法: 1 calBag()是滚动数组 2 calBag2()是逆向填表 #pragma once #include <stdio.h> #include <stdlib.h> #include <vector> using na

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 poss

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-

[再做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

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 weight Wi (1 ≤ Wi 

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

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 ≤

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