用回溯法解决0-1背包问题
来源:互联网 发布:电脑滚动字幕软件 编辑:程序博客网 时间:2024/05/23 02:05
以下是没有剪枝的算法:
public class Knapsack {static double C;//背包容量static double[] P;//物品价值static double[] W;//物品重量static int N;//物品个数static double bestP;static double currP;static double currW;public static void main(String[] args) {N=4;C=7;P=new double[]{9.0,10.0,7.0,4.0};W=new double[]{3.0,5.0,2.0,1.0};double result=test();System.out.println("the best result is"+result);}public static double test(){backTrace(0);return bestP;}public static void backTrace(int t){if(t==N){System.out.println("this branch CurrentValue:"+currP+"CurrentWeight:"+currW);if(currW>C)return;if(currP>bestP)bestP=currP;return;}else{currW+=W[t];currP+=P[t];backTrace(t+1);currW-=W[t];currP-=P[t];backTrace(t+1);}}}
0 0
- 回溯法解决0-1背包问题
- 回溯法解决0-1背包问题
- 回溯法解决0-1背包问题
- 回溯法解决0-1背包问题
- 回溯法解决0-1背包问题
- 回溯法解决0-1背包问题
- 回溯法解决0-1背包问题
- 回溯法解决0-1背包问题
- 用回溯法解决0-1背包问题
- 用回溯法解决0-1背包问题
- 用回溯法解决0-1背包问题
- 用回溯法解决背包问题
- 0-1背包问题之使用回溯法解决
- php回溯法解决0-1背包问题
- Java实现回溯法解决0-1背包问题
- 动态规划与回溯法解决0-1背包问题
- scala 回溯法解决0-1背包问题
- 0-1背包问题(回溯法)
- iptables对报文中TTL的操作
- java动态代理和静态代理
- Python之学习笔记(正则表达式)
- solr分页查询和高亮
- poj 1182 食物链 【带权并查集】
- 用回溯法解决0-1背包问题
- Android之Intent原理
- iOS核心面试题 ~
- Internal Sorting: Radix sort: Sorting by Exchanging
- Python之学习笔记(高级特性)
- 设计模式之二:策略模式(Strategy)
- redis主从配置
- UBUNTU LOG 5-31隐藏Ubuntu分区
- jstack排查定位线程