hdu3033---I love sneakers!(分组背包变形)

本文介绍了一种特殊的分组背包问题,重点在于如何确保每组至少选择一件商品,并通过示例详细展示了算法实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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: zchao1995@gmail.com 
    > 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值