HDU 3033 I love sneakers! (分组背包)

本文介绍了一种特殊的分组背包问题,即确保每个品牌至少购买一件商品的同时,如何在有限预算内最大化总价值。通过详细解释算法思路及提供具体代码实现,帮助读者理解并解决此类问题。

I love sneakers!

Time Limit: 2000/1000 MS (Java/Others)   

Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2683    Accepted Submission(s): 1094

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

思路:普通分组背包问题是每一个组里最多取一件物品,在容量条件下的最优值。而这道题是(1)每一个分组最少取一个;(2)并且可以在每组中取任意件物品。

(1)保证每一个分组至少取一件的方法是,将数组dp[i][j]初始化为-1,其中dp[i][j]表示的是取到第 i 个品牌,花费为 j 的时候的最优值,那么最终要求的值就是dp[K][M](K为品牌数,M为钱)。每次循环的时候判断之前的状态值是否为-1,若是,则说明该状态是不符合要求的(因为没有取到上个品牌的鞋子),当然,要将dp[0][j]初始化为0,因为要保证第一个品牌的鞋子可取。相反,若之前的状态值不是-1,说明该状态是已经被更新过的了,是符合要求的状态。

(2)每个分组的物品可以任意取,只要注意下循环次序就好了。(跟01背包没什么区别)

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define SIZE 10005
 6 #define Size 105
 7 
 8 using namespace std;
 9 
10 struct node
11 {
12     int v,c;
13 };
14 
15 int dp[15][SIZE];
16 node brand[15][Size];
17 int index[Size];
18 int N,M,K;
19 
20 int main()
21 {
22     while(~scanf("%d%d%d",&N,&M,&K))
23     {
24         memset(dp,-1,sizeof(dp));
25         memset(dp[0],0,sizeof(dp[0]));
26         memset(index,0,sizeof(index));
27         for(int i=1; i<=N; i++)
28         {
29             int b,c,v;
30             scanf("%d%d%d",&b,&c,&v);
31             brand[b][++index[b]].c = c;  //记录第b组的每一件物品的花费和价值
32             brand[b][index[b]].v = v;
33         }
34         for(int i=1; i<=K; i++)
35         {
36             for(int k=1; k<=index[i]; k++) 
37             {
38                 for(int j=M; j>=brand[i][k].c; j--)
39                 {
40                     if(dp[i][j-brand[i][k].c] != -1)  //决定取当前第 i 个牌子时要取多少件以及怎样得到最优值
41                         dp[i][j] = max(dp[i][j],dp[i][j-brand[i][k].c]+brand[i][k].v);
42                     if(dp[i-1][j-brand[i][k].c] != -1)  //用于承接上一个品牌时的状态
43                         dp[i][j] = max(dp[i][j],dp[i-1][j-brand[i][k].c]+brand[i][k].v);
44                 }
45             }
46         }
47         if(dp[K][M] < 0)
48             printf("Impossible\n");
49         else
50             printf("%d\n",dp[K][M]);
51     }
52     return 0;
53 }

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值