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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }

 

基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员和用户两个主要模块。管理员负责视频、文件和文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性和性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性和易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解和掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例和技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值