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

来源:互联网 发布:遗传算法ga 编辑:程序博客网 时间:2024/06/03 05:34

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 31 4 62 5 73 4 991 55 772 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|1using 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;}


2 0