I love sneakers!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2860 Accepted Submission(s): 1170
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.
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
Source
Recommend
gaojie
题意:有k个牌子的鞋子→ →,全部牌子都至少拿一种啊,每种鞋子都有价钱和价值,有m元,求最大价值
题解:背包啊....但是由于要全部牌子都至少取一种,所以二维吧,一个维数是种数,一个维数是钱。一开始dp【0】【i】赋值0代表能花i元买了0个牌子,其他全部赋值-1代表都不能取。例如:如果dp【i】【j】==-1,那么就证明这个值无法取出来,所以就不能通过这个值递推下面的值
#include<stdio.h>
#include<string.h>
int val[13][101],mon[13][101];
int num[13],dp[13][10005];
int MAX(int a,int b)
{
if(a>b) return a;
return b;
}
int main()
{
int n,m,kk,x,y,z,i,j,k;
while(scanf("%d%d%d",&n,&m,&kk)>0)
{
memset(num,0,sizeof(num));
memset(dp,-1,sizeof(dp));
for(i=0;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
mon[x][num[x]]=y;
val[x][num[x]++]=z;
}
for(i=0;i<=m;i++) dp[0][i]=0;
for(i=1;i<=kk;i++)
{
for(j=0;j<num[i];j++)
{
for(k=m;k>=mon[i][j];k--)
{
if(dp[i][k-mon[i][j]]!=-1) //这里就保证已经取了i种。2个if不要乱放,多维的背包都是要注意体积为0的情况de~
dp[i][k]=MAX(dp[i][k],dp[i][k-mon[i][j]]+val[i][j]);
if(dp[i-1][k-mon[i][j]]!=-1) //这里也保证dp[i-1][k-mon[i][j]]那个值是可以取出来的
dp[i][k]=MAX(dp[i][k],dp[i-1][k-mon[i][j]]+val[i][j]);
}
}
}
if(dp[kk][m]<0) printf("Impossible\n");
else printf("%d\n",dp[kk][m]);
}
return 0;
}