您的位置:首页 > 其它

HDU 3033 - I love sneakers!(分组背包)

2014-11-02 23:02 274 查看
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

                                                                   

题意:

要求每种型号必须选择至少一样,在经费范围内取得价值最大。

思路:

题目与分组背包的不同是分组背包是每一组取或者不取,而这题是每一组至少取1. 所以要再多开一维来记录组序号。

将每一组的物品当成01背包的进行取。 dp[i][j] :表示买了1~i 组费用为j 是得到的最大价值。

dp[i][k] = max(dp[i][k], dp[i][k - p[i][j]] + v[i][j]);
dp[i][k] = max(dp[i][k], dp[i - 1][k - p[i][j]] + v[i][j]);
一开始要使dp 初始化为-1;

CODE:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;

vector<int> p[105], v[105];
int dp[105][10005];

int main()
{
//freopen("in", "r", stdin);
int n, m, t;
while(~scanf("%d %d %d", &n, &m, &t)){
for(int i = 0; i <= t; i++){
p[i].clear();
v[i].clear();
}
int a, b, c;
for(int i = 0; i < n; ++i){
scanf("%d%d%d", &a, &b, &c);
p[a].push_back(b);
v[a].push_back(c);

}
for(int i = 0; i <= t; ++i){
for(int j = 0; j <= m; ++j){
if(i == 0)
dp[i][j] = 0;
else dp[i][j] = -1;
}
}

for(int i = 1; i <= t; ++i){
for(int j = 0; j < p[i].size(); ++j){
for(int k = m; k >= p[i][j]; --k){
dp[i][k] = max(dp[i][k], dp[i][k - p[i][j]] + v[i][j]);
dp[i][k] = max(dp[i][k], dp[i - 1][k - p[i][j]] + v[i][j]);
}
}
}
if(dp[t][m] > 0)
printf("%d\n", dp[t][m]);
else printf("Impossible\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DP 分组背包 HDU