完全背包

/* 动态规划*/
#include <stdio.h>
#define MAX_ITEMS 101
#define MAX_WEIGHT 101

int weight[MAX_ITEMS];
int value[MAX_ITEMS];
int weightLimit;
int numOfItems;
int result;
int maxValue[MAX_ITEMS][MAX_WEIGHT];

void completeKnapsackV1(){
	int preItems, lowerWeight, totalWeight, tempValue;
	for (preItems = 1; preItems <= numOfItems; preItems++){
		lowerWeight = weight[preItems];
		for (totalWeight = 1; totalWeight <= weightLimit; totalWeight++)
			if (lowerWeight > totalWeight)
				maxValue[preItems][totalWeight] = maxValue[preItems - 1][totalWeight];
			else {
				//注意和01背包的区别是"maxValue[preItems]",也就是说就算选了一次,也可以继续选
				tempValue = maxValue[preItems][totalWeight - lowerWeight] + value[preItems];
				if (tempValue > maxValue[preItems][totalWeight])
					maxValue[preItems][totalWeight] = tempValue;
			}
	}
	result = maxValue[numOfItems][weightLimit];
}

int main(){
	scanf("%d%d", &numOfItems, &weightLimit);
	int item;
	for (item = 1; item <= numOfItems; item++)
		scanf("%d%d", &weight[item], &value[item]);

	completeKnapsackV1();

	printf("%d", result);
	return 0;
}


/* 动态规划加空间优化 */
#include <stdio.h>
#define MAX_ITEMS 101
#define MAX_WEIGHT 101

int weight[MAX_ITEMS];
int value[MAX_ITEMS];
int weightLimit;
int numOfItems;
int result;
int maxValue[MAX_WEIGHT];

void completeKnapsackV2(){
	int preItems, lowerWeight, totalWeight, tempValue;
	for (preItems = 1; preItems <= numOfItems; preItems++){
		lowerWeight = weight[preItems];
		//注意和01背包的区别是从小到大遍历totalWeight
		for (totalWeight = lowerWeight; totalWeight <= weightLimit; totalWeight++){
				tempValue = maxValue[totalWeight - lowerWeight] + value[preItems];
				if (tempValue > maxValue[totalWeight])
					maxValue[totalWeight] = tempValue;
		}
	}
	result = maxValue[weightLimit];
}

int main(){
	scanf("%d%d", &numOfItems, &weightLimit);
	int item;
	for (item = 1; item <= numOfItems; item++)
		scanf("%d%d", &weight[item], &value[item]);

	completeKnapsackV2();

	printf("%d", result);
	return 0;
}

/* 动态规划*/
#include <stdio.h>
#define MAX_ITEMS 101
#define MAX_WEIGHT 101

int weight[MAX_ITEMS];
int value[MAX_ITEMS];
int weightLimit;
int numOfItems;
int result;
int maxValue[MAX_ITEMS][MAX_WEIGHT];

void completeKnapsackV3(){
	int preItems, lowerWeight, totalWeight, items, maxItems, tempValue;
	for (preItems = 1; preItems <= numOfItems; preItems++){
		lowerWeight = weight[preItems];
		for (totalWeight = 1; totalWeight <= weightLimit; totalWeight++)
			maxItems = totalWeight / lowerWeight;
		for (items = 0; items <= maxItems; items++){
			//一个物品可以不选择放进背包,最多可放进totalWeight / lowerWeight个
			tempValue = maxValue[preItems - 1][totalWeight - lowerWeight * items] + value[preItems] * items;
			if (tempValue > maxValue[preItems][totalWeight])
					maxValue[preItems][totalWeight] = tempValue;
		}
	}
	result = maxValue[numOfItems][weightLimit];
}

int main(){
	scanf("%d%d", &numOfItems, &weightLimit);
	int item;
	for (item = 1; item <= numOfItems; item++)
		scanf("%d%d", &weight[item], &value[item]);

	completeKnapsackV3();

	printf("%d", result);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值