HDU 3033 I love sneakers! -- 分组背包 每组最少拿一个

/*
    http://acm.hdu.edu.cn/showproblem.php?pid=3033  I love sneakers!
	分组背包 每组最少拿一个 注意初始化 和 每组物品必须同时处理
*/

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#define CLR(c,v) (memset(c,v,sizeof(c)))
using namespace std;

template <typename _T>
inline _T Max(_T a,_T b){
	return (a>b)?(a):(b);
}
template <typename _T>
inline _T Max(_T a,_T b,_T c){
	return (a>Max(b,c))?(a):(Max(b,c));
}

const int inf = -(1<<30);
const int INF =  (1<<30);
const int M   =  1e4 + 10;
const int CASE = 10 + 2;

int dp[CASE][M];    // dp[i][0]初始化为0 其中i:(0->10), 其余为inf
int v[CASE][M];
int c[CASE][M];

int main(){
	//freopen("in.txt","r",stdin);
	int n_case, max_cost, n_set;
	while(cin >> n_case >> max_cost >> n_set){
		CLR(dp,0);
		for(int j = 0 ; j <= max_cost ; j++){
			dp[0][j] = 0;
		}
		for (int i = 1 ; i <= n_set ; i++){
			for(int j = 0 ; j <= max_cost ; j++){
				dp[i][j] = inf; // 初始化 确保每个集合中 至少哪一个物品
			}
		}
		int brand,cnt[CASE];CLR(cnt,0);
		for (int i = 1 ; i <= n_case ; i++,cnt[brand]++){ // 每一个集合的背包 必须同时处理
			cin >> brand ;
			cin >> c[brand][cnt[brand]] >> v[brand][cnt[brand]];
		}
		for (int i = 1 ; i <= n_set ; i++){ //每一个类型的物品集合,每个集合最少放一个
			for (int j = 0 ; j < cnt[i] ; j++){ // 遍历每个集合中的所有物品
				for(int k = max_cost ; k >= c[i][j] ; k--){ 
					dp[i][k] = Max(dp[i][k], dp[i-1][k-c[i][j]]+v[i][j], dp[i][k-c[i][j]]+v[i][j]);
				}
			}
		}
		if (dp[n_set][max_cost] < 0)
			cout << "Impossible" << endl;
		else
			cout << dp[n_set][max_cost] << endl;
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值