【HDU 3033】【I love sneakers!】

题目:

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

解题思路:

要求的是我们至少每个种类的鞋子都要来一件,这和分组背包很相似,分组背包就是每组至多取一件,更换一下次序里边的循环就好了。

很奇妙的操作。

ac代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define maxn 100005
using namespace std;
//分组背包变形,每组至少取一个 
struct node{
	int val;
	int vum;
}p[10][maxn];

int dp[11][maxn];
int num[11];
int main()
{
	int n,money,k;
	while(scanf("%d%d%d",&n,&money,&k)!=EOF)
	{
		memset(dp,-1,sizeof(dp));
		memset(num,0,sizeof(num));
		for(int i=0;i<n;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			p[a][num[a]].vum=b;
			p[a][num[a]].val=c;
			num[a]++;
		}
		dp[0][0]=0;
		for(int i=0;i<n;i++)
		{
			for(int q=0;q<num[i];q++)
			{//确保每组至少取一个 
				int val=p[i][q].val;
				int vum=p[i][q].vum;
				for(int j=money;j>=vum;j--)//逆序 保证每个物品至多购买一次 
				{
					if(dp[i][j-vum]!=-1)//顺序不能颠倒 
						dp[i][j]=max(dp[i][j],dp[i][j-vum]+val);
					if(dp[i-1][j-vum]!=-1)
						dp[i][j]=max(dp[i][j],dp[i-1][j-vum]+val);
					//顺序不能调转,因为如果代价为0,调转的话,
					//有可能出现先有dp[k][v] = dp[k-1][v-0]+v,
					//再有dp[k][v] =dp[k][v-0]+v = dp[k-1][v-0]+v+v,所以物品取了两次. 
				}
			}
		}
		int maxx=-1;
		for(int i=0;i<=money;i++)
			maxx=max(maxx,dp[k][i]);
		if(maxx==-1)
			printf("Impossible\n");
		else
			printf("%d\n",maxx);
	}
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值