POJ-3624,Charm Bracelet(01背包)

简介: 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


程序代码:  


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100001
int dp[N],w[N],v[N];
int main()
{
  int n,m;
  scanf("%d %d",&n,&m);
  memset(dp,0,sizeof(dp));
  for(int i=0;i<n;i++)
    scanf("%d %d",&v[i],&w[i]);
  for(int i=0;i<n;i++)
  {
    for(int j=m;j>=v[i];j--)
    {
      dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
    }
  }
  printf("%d\n",dp[m]);
  return 0;
}


相关文章
|
存储
poj 3254 Corn Fields (状态压缩dp)
状态压缩dp其实就是用二进制来表示所有的状态,比如这题, 我们在某一行可以这样取0 1 0 1 1 0 1,用1代表取了,0代表没取,因为这点,它的数据量也限制在20以内,所有看到这样数据量的题目可以先考虑一下状态压缩dp。对于有多行的数据,所有状态的总数必然很庞大,而且不用特殊的方法想要存储这些状态是不太现实的。既然每个点只有这两种情况,我们可以用二进制的一位来表示,0 1 0 1 1 0 1就可以表示为二进制0101101也就是十进制的45,如果我们想要枚举6个点的所有状态,我们只需要从0到2^6取其二进制就可以了,并不会重复或是多余。
39 0
poj 1185 炮兵阵地 (状态压缩dp)
如果你是刚刚开始做状态压缩dp,我建议你先看看 poj 3254 Corn Fields 这是一道比这一题更简单,更容易入门的题目。 还有在代码中我用了一个很巧妙的方法求一个数二进制数中1的个数 具体请看我博客中 x& (x - 1)==0 这篇文章 链接 。
49 1
poj 3624 Charm Bracelet(简单01背包)
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.
51 0
|
BI
poj 1976 A Mini Locomotive(01背包)
题目的大概意思就是说给你n个数,然后就是有三辆货车头可以拉连续k辆车厢,问你这三个火车头最终可以拉的最大的乘客数是多少。
52 0
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
|
知识图谱
POJ-1384,Piggy-Bank(完全背包)
POJ-1384,Piggy-Bank(完全背包)
[转]POJ3624 Charm Bracelet(典型01背包问题)
来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ Time Limit: 1000MS          Memory Limit: 65536K  ...
1246 0