完全背包
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; }
相关文章推荐
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- PHP动态规划解决0-1背包问题实例分析
- C#使用动态规划解决0-1背包问题实例分析
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- PHP贪婪算法解决0-1背包问题实例分析
- PHP回溯法解决0-1背包问题实例分析
- LFC1.0.0 版本发布
- 动态规划
- C++ 动态规划
- Android px、dp、sp之间相互转换
- android中像素单位dp、px、pt、sp的比较
- DP(动态规划) 解游轮费用问题
- Android对px和dip进行尺寸转换的方法
- 动态规划的用法——01背包问题
- 动态规划的用法——01背包问题
- 《收集苹果》 动态规划入门
- 《DNA比对》蓝桥杯复赛试题
- 《背包问题》 动态规划