码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 题目3033 I love sneakers!(分组背包)

时间:2015-03-08 14:19:02      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

I love sneakers!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4253    Accepted Submission(s): 1735


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
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  3535 2415 3449 3035 3036 
 
ac代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max(a,b) (a>b?a:b)
struct s
{
	int w,v;
}sn[20][1001];
int num[200],dp[20][10100];
int main()
{
	int N,M,K;
	while(scanf("%d%d%d",&N,&M,&K)!=EOF)
	{
		int i,j,k;
		memset(num,0,sizeof(num));
		for(i=0;i<N;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			sn[a][num[a]].w=b;
			sn[a][num[a]].v=c;
			num[a]++;
		}
		memset(dp,-1,sizeof(dp));
		for(i=0;i<=M;i++)
			dp[0][i]=0;
		for(k=1;k<=K;k++)
		{
			for(i=0;i<num[k];i++)
			{
				for(j=M;j>=sn[k][i].w;j--)
				{
					if(dp[k][j-sn[k][j].w]!=-1)
						dp[k][j]=max(dp[k][j],dp[k][j-sn[k][i].w]+sn[k][i].v);
					if(dp[k-1][j-sn[k][j].w]!=-1)
						dp[k][j]=max(dp[k][j],dp[k-1][j-sn[k][i].w]+sn[k][i].v);
				}
			}
		}
		if(dp[K][M]==-1)
			printf("Impossible\n");
		else
			printf("%d\n",dp[K][M]);
	}
}


HDOJ 题目3033 I love sneakers!(分组背包)

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/44132537

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!