Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5105 Accepted Submission(s): 2122
Total Submission(s): 5105 Accepted Submission(s): 2122
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.
![HDU-3033-I love sneakers! - star - star的博客 HDU-3033-I love sneakers! - star - star的博客](https://i-blog.csdnimg.cn/blog_migrate/3f0658dd4eb6c70bc8ae2e8a18fbf57f.png)
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
题意:有K个品牌的运动鞋,一共N款,(每个品牌有多款运动鞋,但每款只能取一个)总钱数为M,
求不超过总钱数且每个品牌鞋子至少买一双的情况下,使价值最大。
如果 有一款买不到,就输出“Impossible"。
输入多组测试数据:
第一行包含三个数:N (1<=N<=100)表示鞋子款数 ,M(1 <= M<= 10000) 表示总钱数,K(1<=K<=10)表示鞋子品牌数
接下来N行每行一款鞋子的信息:
第一个数:鞋子所属品牌数a (1<=a<=K)
第二个数:鞋子的价格b(0<=b<100000)
第三个数:鞋子的价值c (0<=b<100000)
输出:
每组测试数据输出取得的最大价值,如果有一款鞋子买不到则输出 "Impossible"*/
思路:(某个大神的)
这个题的关键还是在于初始化,如果我们一开始把dp初始化为0,则当所有鞋子的价值都是0时,我们就无法区分是买不全那几款鞋子还是能
买全但最大价值是0;因此,要把S!=0的dp[S][j]初始化为-1,便于区分。这是一个带分组的01背包问题,与普通分组背包不同的是,这个分组背包关键在于每组至少取1个,而不是最多1个。d p[i][k]是不选择当前鞋子;dp[i-1][k-v[j]]+w[j]是选择当前鞋子,但是是第一次在本组中选,由于开始将该组dp赋为了-1,所以第一次取时,必须由上一组的结果推知,
这样才能保证得到全局最优解;dp[i][k-v[j]]+w[j]表示选择当前鞋子,并且不是第一次取。
#include <stdio.h>
#include <string.h>
#include <algorithm>
int dp[11][10005];
int main()
{
using namespace std;
int n,m,k;
int a[105],b[105],c[105];
int i,j,l;
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(dp,-0x3f,sizeof(dp));
for(i=0; i<n; i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
}
for(i=0; i<=m; i++)
{
dp[0][i] = 0;
}
for(i=1; i<=k; i++)
{
for(j=0; j<n; j++)
{
if(a[j] == i)
{
for(l=m; l>=b[j]; l--)
{
dp[i][l] = max(max(dp[i-1][l-b[j]]+c[j] , dp[i][l]) , dp[i][l-b[j]]+c[j]);
}
}
}
}
if(dp[k][m]>=0)
{
printf("%d\n",dp[k][m]);
}
else
{
printf("Impossible\n");
}
}
return 0;
}