您的位置:首页 > 其它

完全背包

2015-10-13 13:39 148 查看
/* 动态规划*/
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息