hdu 2191

题目大意:该题是中文的,读者可以直接去看原题。

题目分析:在sampleinput中,我们可以看到以下内容

Sample Input
1
8 2
2 100 4
4 100 2


1  ------测试用例的个数,用t表示

8  -------在这里理解为背包容量,用V表示

 2  ---------物品种类数,用n表示

 2  ----------物品的价格,这时在这种模型中理解为物品的体积,用c[i]表示。因为你的总钱数作为了背包容量。

100 ---------物品的体积。这里理解为物品的价值。用w[i]表示

 4   -----------物品的数量。用amount[i]表示。


只要理解了01背包,完全背包和多重背包,这一道题数这时比较简单的。。。。


代码如下:

/*
 * 2191.c
 *
 *  Created on: 2013年7月30日
 *      Author: 黄东东 
 *      章泽天是我的女神。。。。。。。。。
 */


#include <stdio.h>
#include <string.h>
int n,V,c[105],w[105],amount[105],f[106];

#define max(a,b) ((a>b)?(a):(b))
#define min(a,b) ((a>b)?(b):(a))
void ZeroOnePack(int cost , int weight){
	int v;
	for( v = V ; v >= cost ; --v){

		f[v] = max(f[v],f[v - cost] +weight);
	}
}

void CompletePack(int cost ,int weight){
	int v;
	for(v = cost ; v<= V ; ++v){
		f[v] = max(f[v],f[v-cost]+ weight);
	}
}

void MultiplePack(int cost ,int weight , int amount){

	if(cost*amount > V){
		CompletePack(cost,weight);
	}

	int k = 1;

	while( k < amount){

		ZeroOnePack(k*cost,k*weight);

		amount -= k;

		k<<=1;
	}
	ZeroOnePack(amount*cost,amount*weight);

}
int main(){


	int t ;

	scanf("%d",&t);
	while( t-- ){
		scanf("%d%d",&V,&n);
		int i ;
		for(i = 0 ; i < n ; ++i){
			scanf("%d%d%d",&c[i],&w[i],&amount[i]);
		}

		memset(f,0,sizeof(f));
		for(i = 0 ; i < n ;++i){
			MultiplePack(c[i],w[i],amount[i]);
		}


		printf("%d\n",f[V]);

	}
	return 0;
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值