HDU 3033 I love sneakers! 分组背包变形

Description

After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store. 

There are several brands of sneakers that Iserlohn wants to collect, such as Air Jordan and Nike Pro. And each brand has released various products. For the reason that Iserlohn is definitely a sneaker-mania, he desires to buy at least one product for each brand. 
Although the fixed price of each product has been labeled, Iserlohn sets values for each of them based on his own tendency. With handsome but limited money, he wants to maximize the total value of the shoes he is going to buy. Obviously, as a collector, he won’t buy the same product twice. 
Now, Iserlohn needs you to help him find the best solution of his problem, which means to maximize the total value of the products he can buy.
 

Input

Input contains multiple test cases. Each test case begins with three integers 1<=N<=100 representing the total number of products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, b and c, 0<=b,c<100000, meaning the brand’s number it belongs, the labeled price, and the value of this product. Process to End Of File.
 

Output

For each test case, print an integer which is the maximum total value of the sneakers that Iserlohn purchases. Print "Impossible" if Iserlohn's demands can’t be satisfied.
 

Sample Input

       
       
5 10000 3 1 4 6 2 5 7 3 4 99 1 55 77 2 44 66
 

Sample Output

       
       
255
 
分组背包的变形

分组背包的解释:

问题

N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。这些物品被划分为若干组,每组中的物品互相冲突,最多选一件。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。

算法

这个问题变成了每组物品有若干种策略:是选择本组的某一件,还是一件都不选。也就是说设f[k][v]表示前k组物品花费费用v能取得的最大权值,则有:

f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]|物品i属于组k}

使用一维数组的伪代码如下:

for 所有的组k
    for v=V..0
        for 所有的i属于组k
            f[v]=max{f[v],f[v-c[i]]+w[i]}

注意这里的三层循环的顺序,甚至在本文的第一个beta版中我自己都写错了。“for v=V..0”这一层循环必须在“for 所有的i属于组k”之外。这样才能保证每一组内的物品最多只有一个会被添加到背包中

另外,显然可以对每组内的物品应用P02中“一个简单有效的优化”。

小结

分组的背包问题将彼此互斥的若干物品称为一个组,这建立了一个很好的模型。不少背包问题的变形都可以转化为分组的背包问题(例如P07),由分组的背包问题进一步可定义“泛化物品”的概念,十分有利于解题。




题意:
经过几个月的努力,Iserlohn终于获得了一笔数量可观的奖学金。作为一名运动鞋狂热者,他打算把他全部的钱花在运动鞋商店里。
这里有几种Iserlohn想收集的运动鞋品牌,向乔丹和耐克。每一个牌子都有几种产品。由于Iserlohn对运动鞋有绝对的狂热,他希望每种牌子至少买一双。
尽管每双鞋都有确定的标价,Iserlohn仍然按照他自己的喜好给每双鞋定了价值。带着可观但是有限的钱,他想使他买的东西的总价值最大。显然,作为一个收藏者,他不会同一双鞋买两次。

n-物品总数 ,m 钱数, k 牌子数 ,描述 a 牌号 b标价 c价值

每种牌子都要买到,否则impossible

单纯的分组背包必选情况,特别的是物品有0花费的情况,0花费要先放在当前层,再放在上一层才不会买两次,即先买0花费的




借鉴了别人的思路:

计状态dp[i][j]代表前i组容量为j的最大价值。由于一组里面有多个物品,所以状态转移可以是前一组少取一个,即dp[i-1][p-g[i][j].v]+g[i][j].w,也可以是当前组之前去过的少取一种,即dp[i][p-g[i][j].v]+g[i][j].w。

    网上有些解题报告是错误的解法,这题dp初始化的时候要初始化为负无穷,因为这题要求的是上一组恰好达到的状态才能转移到这一组来,因为每一组至少得去一个。当然可以初始化为-1,每次判断一下是不是-1就行。

本题目不是要求每组最多一个所以不用按照背包九讲中说的for循环的顺序
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct node
{
    int w,p;
} a[15][105];
int main()
{
    int n,cash,m;
    int num[15],dp[15][10005];
    while(~scanf("%d %d %d",&n,&cash,&m))
    {
        memset(dp,-1,sizeof(dp));
        memset(num,0,sizeof(num));
        for(int i=1; i<=n; i++)
        {
            int d,b,c;
            scanf("%d %d %d",&d,&b,&c);
            a[d][num[d]].w=b;
            a[d][num[d]].p=c;
            num[d]++;
        }
        memset(dp[0],0,sizeof(dp[0]));
        for(int i=1; i<=m; i++)
        {
            for(int j=0; j<num[i]; j++)
                for(int k=cash;k>=a[i][j].w; k--)
                {
                    if(dp[i][k]!=-1)
                        dp[i][k]=max(dp[i][k],dp[i][k-a[i][j].w]+a[i][j].p);
                    if(dp[i-1][k]!=-1)
                        dp[i][k]=max(dp[i][k],dp[i-1][k-a[i][j].w]+a[i][j].p);
                    }
        }
        if(dp[m][cash]==-1)
            printf("Impossible\n");
        else

            printf("%d\n",dp[m][cash]);
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
    int w,p;
} a[15][105];
int main()
{
    int n,cash,m;
    int num[15],dp[15][10005];
    while(~scanf("%d %d %d",&n,&cash,&m))
    {
        for(int i=1;i<=m;i++)
          for(int j=0;j<=cash;j++)
          dp[i][j]=-INF;
        memset(dp,-1,sizeof(dp));
        memset(num,0,sizeof(num));
        for(int i=1; i<=n; i++)
        {
            int d,b,c;
            scanf("%d %d %d",&d,&b,&c);
            a[d][num[d]].w=b;
            a[d][num[d]].p=c;
            num[d]++;
        }
        memset(dp[0],0,sizeof(dp[0]));
        for(int i=1; i<=m; i++)
        {
            for(int j=0; j<num[i]; j++)
                for(int k=cash; k>=a[i][j].w; k--)
                {
                    dp[i][k]=max(dp[i][k-a[i][j].w]+a[i][j].p,max(dp[i][k],dp[i-1][k-a[i][j].w]+a[i][j].p));
                }
        }
        if(dp[m][cash]<0)
            printf("Impossible\n");
        else

            printf("%d\n",dp[m][cash]);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值