五、算法_动态规划_背包01
来源:互联网 发布:霍金对人工智能的看法 编辑:程序博客网 时间:2024/05/17 08:19
动态规划:背包问题01位
问题:有一个m KG的背包和物品 {
T1 、T2 、T3 、Tn },每件物品价格{P1 、P2 、P3 、Pn },现在要把所有最大价值的物品放进背包中,且每个物品只能放入一次(不能超出背包重量),其中所有的条件都是已知的,求最大价值;
穷举法:
using System;namespace 动态规划_背包穷举 { class Program { static void Main (string[] args) { int pMKG ; int[] pWkg = new[] { 0, 3, 4, 5 }; int[] pPrices = new[] { 0, 4, 5, 6 }; Console.WriteLine(Exhaustivity(10, pWkg, pPrices)); Console.WriteLine(Exhaustivity(3, pWkg, pPrices)); Console.WriteLine(Exhaustivity(4, pWkg, pPrices)); Console.WriteLine(Exhaustivity(5, pWkg, pPrices)); Console.WriteLine(Exhaustivity(7, pWkg, pPrices)); Console.WriteLine(); Console.ReadKey(); } // ------------穷举法------------ public static int Exhaustivity (int mKg, int[] wKg, int[] prices) { // 物品个数 int mRefI = wKg.Length-1; int pMaxPrices = 0; for (int i = 0; i < Math.Pow(2, mKg); i++) { // 重量和 int pWeightTotal = 0; // 价格和 int pPricesTotal = 0; // 取得 i 上某一位的二进制值 for (int j = 1; j <= mRefI; j++) { int pResult = Get(i, j); if (pResult == 1) { // 物品的价格和重量 pWeightTotal += wKg[j]; pPricesTotal += prices[j]; } } // 设置最大价值,和判断背包是否放得下物品 if (pWeightTotal <= mKg && pPricesTotal > pMaxPrices) { pMaxPrices = pPricesTotal; } } return pMaxPrices; } // 取得 i 上第几位上的二进制值,是1或者0 // 按位与运算 public static int Get (int pNumber, int pIndex) { int a = pNumber; int b = (int)Math.Pow(2, pIndex - 1); int pRef = a & b; if (pRef == 0) { return 0; } return 1; } }}
动态规划(自底向上):
namespace 动态规___自底向上 { class Program { static void Main (string[] args) { int pMKG; int[] pWkg = new[] { 0, 3, 4, 5 }; int[] pPrices = new[] { 0, 4, 5, 6 }; Console.WriteLine(BottomUp(10,3, pWkg, pPrices)); Console.WriteLine(BottomUp(3,3, pWkg, pPrices)); Console.WriteLine(BottomUp(4,3, pWkg, pPrices)); Console.WriteLine(BottomUp(5,3, pWkg, pPrices)); Console.WriteLine(BottomUp(7,3, pWkg, pPrices)); Console.WriteLine(); Console.ReadKey(); } public static int[,] pResult = new int[11, 4]; public static int BottomUp (int mKg, int pIndex, int[] wKg, int[] pPrices) { if (pResult[mKg, pIndex] != 0) return pResult[mKg, pIndex]; for (int i = 0; i < mKg + 1; i++) { for (int j = 1; j < pIndex + 1; j++) { if (pResult[i, j] != 0) continue; if (wKg[j] > i) { pResult[i, j] = pResult[i, j - 1]; } else { int pMaxValue1 = pResult[i - wKg[j], j - 1] + pPrices[j]; int pMaxValue2 = pResult[i, j - 1]; if (pMaxValue1 > pMaxValue2) { pResult[i, j] = pMaxValue1; } else { pResult[i, j] = pMaxValue2; }// End if }// End if } }// End for return pResult[mKg, pIndex]; }// End bottomup }}
0 0
- 五、算法_动态规划_背包01
- 算法_动态规划_二维01背包问题
- 蓝桥杯_算法提高_金明的预算方案(动态规划、01背包变形)
- 动态规划_背包问题
- 蓝桥杯_算法提高_01背包(动态规划算法)
- 夕拾算法进阶篇:18)装箱问题 (01背包_动态规划DP)
- 夕拾算法进阶篇:19)采药 (01背包_动态规划DP)
- 算法_动态规划_货币兑换
- 算法_动态规划_图像压缩
- 算法_动态规划_电路布线
- 算法_动态规划_流水作业调度
- 算法_动态规划_漂亮打印
- 算法_动态规划总结
- 算法设计_动态规划
- 四、算法_动态规划
- 动态规划8_多重背包
- 动态规划9_完全背包
- 0-1背包_动态规划_java
- 为hadoop集群设置静态IP
- 递归 与 迭代 的区别对比!
- Net设计模式实例之中介者模式(Mediator Pattern)
- 向量组/矩阵/秩的理解
- C++之DLL学习
- 五、算法_动态规划_背包01
- HDU 1312 Red and Black
- Spring Security系列四 自定义决策管理器(动态权限码)
- 拉马努金恒等式
- Net设计模式实例之迭代器模式(Iterator Pattern)
- Redis学习-配置
- 正则表达式
- case when then else end用法
- HTML学习01