HDU 3033 I love sneakers!(01背包变形)

I love sneakers!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4538    Accepted Submission(s): 1866


Problem 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
 


题目的难点就在于每种牌子至少选一种。网上好多人说这道题属于分组背包,但我认为这道题只是一道加了条件限制的01背包。但这个不重要(手动斜眼)。

首先把各个品牌的鞋子放到各自对应的vector中,然后遍历每种品牌的每双鞋,再然后就是我们熟悉的for(int k=V;k>=brand[i][j].w;k--)了。但在状态转移的时候我们要加上一个判断,从当前我们要选的品牌转移来的状态不用判断,因为是否选择当前的一双鞋与本组内是否有鞋被选无关。而从上一组转移状态要加上一个if判断句,判断上一组是否有鞋被选中,如果没有购买上一个品牌的鞋子,那么这一个品牌的鞋子就谈不上购买了,因为题目规定每种品牌至少购买一双。

#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma commment(linker,"/STACK: 102400000 102400000")
#define lson a,b,l,mid,cur<<1
#define rson a,b,mid+1,r,cur<<1|1
using namespace std;
typedef long long LL;
const double eps=1e-6;
const int MAXN=1e5+50;
struct sneaker
{
    int w,v;
    sneaker(int w,int v)
    {
        this->w=w;
        this->v=v;
    }
};

vector<sneaker>brand[12];
int n,V,b,dp[12][MAXN];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    while(scanf("%d%d%d",&n,&V,&b)!=EOF)
    {
        for(int i=0;i<12;i++)
            brand[i].clear();
        for(int i=1;i<=n;i++)
        {
            int t1,t2,t3;
            scanf("%d%d%d",&t1,&t2,&t3);
            brand[t1].push_back(sneaker(t2,t3));
        }
        memset(dp,-1,sizeof(dp));//首先把全体状态初始化为-1,表示当前哪个品牌的鞋子都没有购买
        memset(dp[0],0,sizeof(dp[0]));//把dp[0]数组初始化为0,以便状态转移
        for(int i=1;i<=b;i++)
        {
            for(int j=0;j<brand[i].size();j++)
            {
                for(int k=V;k>=brand[i][j].w;k--)
                {
                    //if(dp[i][k-brand[i][j].w]!=-1)//加与不加无所谓,是否购买当前的鞋子与当前品牌与没有被购买过无关,只与上一个品牌有关
                    dp[i][k]=max(dp[i][k],dp[i][k-brand[i][j].w]+brand[i][j].v);
                    if(dp[i-1][k-brand[i][j].w]!=-1)//如果上一个品牌没有购买,当前品牌就无法购买
                        dp[i][k]=max(dp[i][k],dp[i-1][k-brand[i][j].w]+brand[i][j].v);
                }
            }
        }
        if(dp[b][V]<0)
            printf("Impossible\n");
        else
            printf("%d\n",dp[b][V]);
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值