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
Source
2009 Multi-University Training Contest 13 - Host by HIT
Recommend
gaojie | We have carefully selected several similar problems for you: 2639 2844 3535 2415 3449
Statistic | Submit | Discuss | Note
这是一道分组背包的题,与普通分组背包不同的是,它要求每组的物品至少取一件而不是最多只可以取一件
我们设状态dp[i][j]表示选到第i组,花费小于等于j元的情况下,最多可以得到的价值
那么dp[i][j]有2个前驱状态,第一种是此时没有选过第i组的;第二种是此时已经选过第i组的,这样就解决了至少选1件
由于价值可以是0,所以初始化不能是0,不然最后无法判断是否是impossible,可以初始化为一个负数
由于是可以选多件,所以循环顺序就和普通的分组背包不同了
/*************************************************************************
> File Name: hdu3033.cpp
> Author: ALex
> Mail: [email protected]
> Created Time: 2015年05月10日 星期日 14时16分31秒
************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
int dp[11][10010];
vector <PLL> val[11];
int main() {
int n, m, k;
while (~scanf("%d%d%d", &n, &m, &k)) {
memset(dp, -inf, sizeof(dp));
for (int i = 1; i <= n; ++i) {
val[i].clear();
}
int a, b, c;
for (int i = 1; i <= n; ++i) {
scanf("%d%d%d", &a, &b, &c);
val[a].push_back(make_pair(b, c));
}
for (int i = 0; i <= m; ++i) {
dp[0][i] = 0;
}
for (int i = 1; i <= k; ++i) {
int size = val[i].size();
for (int j = 0; j < size; ++j) {
for (int v = m; v >= 0; --v) {
if (v < val[i][j].first) {
break;
}
if (dp[i][v - val[i][j].first] != -inf) {
dp[i][v] = max(dp[i][v], dp[i][v - val[i][j].first] + val[i][j].second);
}
if (dp[i - 1][v - val[i][j].first] != -inf) {
dp[i][v] = max(dp[i][v], dp[i - 1][v - val[i][j].first] + val[i][j].second);
}
}
}
}
if (dp[k][m] < 0) {
printf("Impossible\n");
continue;
}
printf("%d\n", dp[k][m]);
}
return 0;
}