一、内容
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
二、思路
- 分成几个组,每个组至少选择一个物品,求最大价值是多少。
- 常见的分组背包问题是多个组别,每个组别最多选一个物品,而这道题是至少选一个物品。
- 我们来考虑状态设定:dp[k][j] 代表已经选择了前k个组,且刚好凑成j的最大价值。
- 那么当我们选择K组中某个物品的时候,它可以由2个状态转移过来。
(1)从上一组转移过来。 dp【k - 1】【j - v】 + w
(2)从本组的上一个物品转移过来。 dp【k】【j - v】 + w
若上一个状态不是-1, 那么代表上一个状态是合法状态,才能进行转移。
所以我们只需要求出 dp【k】【j】,dp【k - 1】【j - v】 + w,dp【k】【j - v】 + w 三者之中的最大值即可。 - 由于题目价值可以为0, 那么上面三个状态可能会变成 dp【k】【j】,dp【k - 1】【j 】 + w,dp【k】【j 】 + w, 那么求解的时候就要注意顺序关系了, 不然可能导致多加了一个w。
三、代码
#include <cstdio>
#include <vector>
#include <cstring>
#define max(a, b) (a > b ? a : b)
using namespace std;
const int M = 10005, N = 105;
int n, m, noNum, dp[15][M];
vector<int> v[N], w[N];
int main() {
while (scanf("%d%d%d", &n, &m, &noNum) != EOF) {
memset(dp, -1, sizeof(dp));
dp[0][0] = 0;
for (int i = 1; i <= noNum; i++) {
v[i].clear(); w[i].clear();
}
int no, a, b;
for (int i = 1; i <= n; i++) {
scanf("%d%d%d", &no, &a, &b);
v[no].push_back(a);
w[no].push_back(b);
}
//2种状态 1.上一次选的是上一组的 2.上一次选的是这一组的
for (int k = 1; k <= noNum; k++) {
for (int i = 0; i < v[k].size(); i++) {
for (int j = m; j >= v[k][i]; j--) {
//这里是求 dp[k][j] 和 dp[k][j - v[k][i]] + w[k][i] 和 dp[k - 1][j - v[k][i]] + w[k][i]的最大值
//但是v可能为0 就变成 dp[k][j], dp[k][j] + w , dp[k - 1][j] + w
//这样的话如果先对k-1进行比较那么就可能多加以一个w
if (dp[k][j - v[k][i]] != -1) {
dp[k][j] = max(dp[k][j], dp[k][j - v[k][i]] + w[k][i]);
}
if (dp[k - 1][j - v[k][i]] != -1) {
dp[k][j] = max(dp[k][j], dp[k - 1][j - v[k][i]] + w[k][i]);
}
}
}
}
//找到不是-1的点的最大价值
int ans = -1;
for (int i = m; i >= 0; i--) {
if (dp[noNum][i] != -1) {
ans = max(ans, dp[noNum][i]);
}
}
if (ans == -1) {
printf("Impossible\n");
} else {
printf("%d\n", ans);
}
}
return 0;
}