POJ 4131:Charm Bracelet(01背包问题)

4131:Charm Bracelet

总时间限制: 

1000ms

内存限制: 

65536kB

描述

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

输入

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

输出

Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints

样例输入

4 6
1 4
2 6
3 12
2 7

样例输出

23

01背包是n件物品(每件物品只有一个)放在容量为c的背包中,能放入物品的最大价值。

我们创建一个二维数组dp[i][j]来表示我们放置第i件物品,背包容量还剩余j,那么对于第i件物品,我们是选还是不选呢?

1.当这件物品的体积大于背包容量时,自然就不选了,那么就有dp[i][j]=d[i-1][j];

2.当这件物品的体积小于背包容量时,我们可以选择,也可以不选择,这时候就要比较了。那么就有

dp[i][j]=max(dp[i-1][j],dp[i-1][j-c[i]]+v[i]);

根据这个题目我们可以写出如下代码(TLE):

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
int c[13010],v[13010],dp[13010][13010];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            scanf("%d%d",&c[i],&v[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                if(j>=c[i])//当前容量能放下这个物品
                    dp[i][j]=max(dp[i-1][j],dp[i-1][j-c[i]]+v[i]);
                else//当前容量不能放下这个物品
                    dp[i][j]=dp[i-1][j];
        }
        printf("%d\n",dp[n][m]);
    }
    return 0;
}

我们可以看到,这个题目时间上应该不能再优化了,从状态转移方程可以看出,我们每次求dp[i][j]的时候,只是用到它上面那一行的代码,所以我们可以用滚动数组,把dp变成一维的。这样在空间上就优化了。

先贴代码(AC):

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
int c[13010],v[13010],dp[13010];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));//这个不要一不小心写在了循环外面
        for(int i=1;i<=n;i++)
            scanf("%d%d",&c[i],&v[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=m;j>=c[i];j--)
                dp[j]=max(dp[j],dp[j-c[i]]+v[i]);
        }
        printf("%d\n",dp[m]);
    }
    return 0;
}

这里为什么要用逆序呢?大家可以看下面的表格
0 4 4  4   4   4   4       放入物品1的收益
0 4 6 10 10 10 10      放入物品2的收益
0 4 6 12 16 18 22      放入物品3的收益
0 4 7 12 16 19 23      放入物品4的收益

以前两行为例,处理dp[2][6]时,就是选择物品2是否要放入背包的过程,此时前面5种背包的状态还是未放入物品2的状态,所以它们此时都是表示上一行的状态。dpi[j]=max(dp[j],dp[j-c[i]]+v[i]);表示的就是dp[i][j]=max(dp[i-1][j],dp[i-1][j-c[i]]+v[i]);如果是顺序的话,在计算dp[i][j]时,前面都是已经放过物品2的状态了,那么 dp[j]=max(dp[j],dp[j-c[i]]+v[i]);表示的就是dp[i][j]=max(dp[i][j],dp[i][j-c[i]]+v[i]);

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值