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 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.
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.
5 10000 3 1 4 6 2 5 7 3 4 99 1 55 77 2 44 66
255
开始写始终WA,初始化有问题。。。看题解才发现原来选择的顺序也有讲究,又长见识了。
题意:一些鞋子有k种品牌,第k种牌子的鞋子里面有一些品种不一样的鞋子,每种都有本身的价格和价值,收藏家有m元想收集每种品牌的鞋子至少一双(可是相同品牌相同品种的鞋他只要一双),问他总共能够收集多少价值的鞋子。
分析:有k个品牌,分组背包问题,《背包九讲》是每组最多选一件,这里不一样,是选至少一件,咱们对每组
进行01背包就好了.咱们当前选第k组的时候在里面选择物品的时候是选的第几件.若是咱们当前是选第k组中的第一件,那么是从第k-1组推过来的,若是不是取第一件(i),
那么就是第k组中的前一件(i-1)推过来的.
如下内容就是这题的精(da)华(keng)了:
可是这里有个很是重要的问题,咱们写成要写成以下形式两个if不能调转:
if(dp[k][v-price[i]] != -1)
dp[k][v] = max(dp[k][v] , dp[k][v - price[i]] + value[i]);
if(dp[k-1][v-price[i]] != -1 )
dp[k][v] = max(dp[k][v] , dp[k-1][v-price[i]] + value[i]);
解释:
顺序不能调转,由于若是代价为0,调转的话,有可能出现先有dp[k][v] = dp[k-1][v-0]+v,再有dp[k][v] =dp[k][v-0]+v = dp[k-1][v-0]+v+v,因此物品取了两次.
固然一种方便的解决办法是直接写个函数 a = max(a,b,c);
还有个重要的地方是初始化:
若是咱们一开始把dp初始化为0,则当全部鞋子的价值都是0时,咱们就没法区分是买不全那几款鞋子仍是能买全但最大价值是0
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 10005
using namespace std;
int dp[105][N]; ///dp[k][i] 表明在前k组中花费i取得的最大价值
int a[105],price[105],value[105];
int main()
{
int n,m,K;
while(scanf("%d%d%d",&n,&m,&K)!=EOF)
{
for(int i=1; i<=n; i++)
{
scanf("%d%d%d",&a[i],&price[i],&value[i]);
}
memset(dp,-1,sizeof(dp)); ///-1表示状态不合法
for(int i=0; i<=m; i++) dp[0][i]=0;
for(int k=1; k<=K; k++)
{
for(int i=1; i<=n; i++)
{
if(a[i]==k)
for(int v=m; v>=price[i]; v--)
{
if(dp[k][v-price[i]] != -1)
dp[k][v] = max(dp[k][v] , dp[k][v - price[i]] + value[i]);
if(dp[k-1][v-price[i]] != -1 )
dp[k][v] = max(dp[k][v] , dp[k-1][v-price[i]] + value[i]);
}
}
}
if(dp[K][m]==-1) printf("Impossible\n");
else printf("%d\n",dp[K][m]);
}
return 0;
}