HDU 3033——I love sneakers!【分组背包变形】【数据 & 条件的透彻分析】

34 篇文章 1 订阅

题目传送门

分组背包变形,每组都需要选,组内随便选


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


题意:

n双鞋子属于k个品牌,每双鞋子有一个价格和价值,问在钱数为m的情况下每个牌子至少买一个,每双鞋子至多买一个时的最大总价值


分析:

  • 分组背包,设dp[i][j]为用了j钱买了前i个品牌时的最大价值,转移时分两种情况
    1.当前品牌不是第一次买 => dp[i][j] = max(dp[i][j], dp[i][j - w[i]] + val[i])
    2.当前品牌是第一次买 => dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + val[i])
    上面的顺序不能颠倒,因为代价若为0,则会出现同一双买两次的情况
    初始时dp[0][j]为0,其余均为-1,且每次转移时必须从非-1态转移

注:

  • 本题后台数据太弱了? ,缺少一种情况:钱数不足以购买全部种类的鞋子~~
  • 注意顺序!!!顺序!!!
  • 第二个if决定了当前组一定由前一组可行解(非-1)得到,因为如果前一组没有可行解,即说明前一组一个物品都没有选,那么答案一定为Impossible,不应该更新当前dp,应保持-1.
  • 第一个if也是必须存在的,这个比较难以分析。第一个if决定了只有当本组存在可选物品时才会进行本组内物品的比较和更新;显然:本组想要有可行解,一定是先通过第二个if更新,即由上一组可行解推算得到。这样就保证了,如果当前组存在可选物品,一定是可行解推算得到,并非是因为存款够多,如果当前组暂没有可行解,那么没有必要对组内的物品进行比较和更新。

AC代码:

#include <iostream>
#include <vector>
#include <utility>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <fstream>
#include <set>
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f;
const int MAXN = 1e4 + 100;

int max(int a, int b, int c) {
	int t = a > b ? a : b;
	return t > c ? t : c;
}
struct node {
	int w, v;
	node(int a, int b) {
		w = a;
		v = b;
	}
};
int dp[12][MAXN];
vector<node>vec[MAXN];

int main() {
	int n, m, kk;
	while (cin >> n >> m >> kk) {
		for (int i = 0; i <= kk; i++) {
			vec[i].clear();
		}
		for (int i = 1; i <= n; i++) {
			int a, b, c;
			cin >> a >> b >> c;
			vec[a].push_back(node(b, c));
		}
		memset(dp, -1, sizeof(dp));
		memset(dp[0], 0, sizeof(dp[0]));

		for (int k = 1; k <= kk; k++) {
			for (int i = 0; i < vec[k].size(); i++) {
				for (int j = m; j >= vec[k][i].w; j--) {
					if (dp[k][j - vec[k][i].w] != -1)
						dp[k][j] = max(dp[k][j], dp[k][j - vec[k][i].w] + vec[k][i].v);
					if (dp[k - 1][j - vec[k][i].w] != -1)
						dp[k][j] = max(dp[k][j], dp[k - 1][j - vec[k][i].w] + vec[k][i].v);
				}
			}
		}
		if (dp[kk][m] == -1)
			cout << "Impossible" << endl;
		else
			cout << dp[kk][m] << endl;
	}

	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值