经过几个月的努力,Iserlohn终于获得了一笔数量可观的奖学金。作为一名运动鞋狂热者,他打算把他全部的钱花在运动鞋商店里。
这里有几种Iserlohn想收集的运动鞋品牌,向乔丹和耐克。每一个牌子都有几种产品。由于Iserlohn对运动鞋有绝对的狂热,他希望每种牌子至少买一双。
尽管每双鞋都有确定的标价,Iserlohn仍然按照他自己的喜好给每双鞋定了价值。带着可观但是有限的钱,他想使他买的东西的总价值最大。显然,作为一个收藏者,他不会同一双鞋买两次。
这里有几种Iserlohn想收集的运动鞋品牌,向乔丹和耐克。每一个牌子都有几种产品。由于Iserlohn对运动鞋有绝对的狂热,他希望每种牌子至少买一双。
尽管每双鞋都有确定的标价,Iserlohn仍然按照他自己的喜好给每双鞋定了价值。带着可观但是有限的钱,他想使他买的东西的总价值最大。显然,作为一个收藏者,他不会同一双鞋买两次。
n-物品总数 ,m 钱数, k 牌子数 ,描述 a 牌号 b标价 c价值
每种牌子都要买到,否则impossible
单纯的分组背包必选情况,特别的是物品有0花费的情况,0花费要先放在当前层,再放在上一层才不会买两次,即先买0花费的
I love sneakers!
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 2
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.
![](https://i-blog.csdnimg.cn/blog_migrate/ef35afe76885c46ac5bbfbb33553acea.jpeg)
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
几个数据
5 10000 3
1 4 6
2 5 7
3 4 99
1 55 77
2 44 66
255
3 5 3
1 2 5
2 2 1
3 2 2
Impossible
5 65 3
1 4 6
2 5 7
3 4 99
1 55 77
2 44 66
183
3 5 3
1 2 5
2 2 1
3 2 2
Impossible
3 5 3
1 0 5
2 0 1
3 0 2
1 4 6
2 5 7
3 4 99
1 55 77
2 44 66
255
3 5 3
1 2 5
2 2 1
3 2 2
Impossible
5 65 3
1 4 6
2 5 7
3 4 99
1 55 77
2 44 66
183
3 5 3
1 2 5
2 2 1
3 2 2
Impossible
3 5 3
1 0 5
2 0 1
3 0 2
#include<stdio.h>
#include<string.h>
#define msize 10010
#define bsize 15
#define nsize 110
int dp[bsize][msize];
int c[bsize][nsize],w[bsize][nsize];
int num[bsize];
int n,m,b;
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,k,l;
while(scanf("%d %d %d",&n,&m,&b)!=EOF)
{
memset(num,0,sizeof(num));
memset(dp,-1,sizeof(dp));
memset(dp[0],0,sizeof(dp[0]));
memset(w,0,sizeof(w));
memset(c,0,sizeof(c));
for(i=0;i<n;i++)
{
int a,e,d;
scanf("%d %d %d",&a,&e,&d);
c[a][num[a]]=e;
w[a][num[a]]=d;
num[a]++;
}
for(i=1;i<=b;i++) //第i个牌
{
for(j=0;j<num[i];j++)//i牌第j个物品
{
for(k=m;k>=c[i][j];k--)//花k钱
{
if(dp[i][k-c[i][j]]!=-1)
{
dp[i][k]=max(dp[i][k],dp[i][k-c[i][j]]+w[i][j]); //避免0花费买重复,当前先买0
}
if(dp[i-1][k-c[i][j]]!=-1)
{
dp[i][k]=max(dp[i][k],dp[i-1][k-c[i][j]]+w[i][j]);//上一层再买0也无法更新dp
}
}
}
}
if(dp[b][m]!=-1)
printf("%d\n",dp[b][m]);
else
printf("Impossible\n");
}
return 0;
}