当前位置: 首页 > news >正文

工程建设标准最新查询网站国际新闻今天最新消息

工程建设标准最新查询网站,国际新闻今天最新消息,网站备案需要审核多久,wordpress站点预览实验五 分支限界法 01背包问题的分治限界法的实现 剪枝函数 限界函数 1.实验目的 1、理解分支限界法的剪枝搜索策略#xff0c;掌握分支限界法的算法框架 2、设计并实现问题#xff0c;掌握分支限界算法。 2.实验环境 java 3.问题描述 给定n种物品和一背包。物品i的重…实验五  分支限界法 01背包问题的分治限界法的实现 剪枝函数 限界函数 1.实验目的 1、理解分支限界法的剪枝搜索策略掌握分支限界法的算法框架 2、设计并实现问题掌握分支限界算法。 2.实验环境 java 3.问题描述 给定n种物品和一背包。物品i的重量是wi其价值为vi背包的容量为C。问应如何选择装入背包的物品使得装入背包中物品的总价值最大? 4.复杂度 算法整体的时间复杂度主要由回溯操作的时间复杂度决定在最坏情况下为O(2^n)。然而在实际应用中由于剪枝操作的存在实际的分支数会远远小于2^n因此算法的执行时间通常会有所缩减。 5.算法实现 package shiyan5;import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner;class Item {int weight;int value;double density; // 物品的单位价值价值重量比public Item(int weight, int value) {this.weight weight;this.value value;this.density (double) value / weight;} }public class BranchAndBound {static ListItem items;static int capacity;static int maxTotalValue;static ListItem selectedItems;public static void main(String[] args) {readInputData(input.txt);branchAndBound();writeOutputData(output.txt);}private static void readInputData(String filename) {try {File file new File(filename);Scanner scanner new Scanner(file);int n scanner.nextInt();capacity scanner.nextInt();items new ArrayList();for (int i 0; i n; i) {int weight scanner.nextInt();int value scanner.nextInt();items.add(new Item(weight, value));}scanner.close();} catch (IOException e) {e.printStackTrace();}}private static void branchAndBound() {Collections.sort(items, (a, b) - Double.compare(b.density, a.density)); // 按单位价值从大到小排序maxTotalValue 0;selectedItems new ArrayList();backtrack(0, 0, 0);// 按照物品的原始顺序排序Collections.sort(selectedItems, (a, b) - Integer.compare(items.indexOf(a), items.indexOf(b)));}private static void backtrack(int level, int currentWeight, int currentValue) {if (level items.size() || currentWeight capacity) {if (currentValue maxTotalValue) {maxTotalValue currentValue;selectedItems.clear();for (int i 0; i level; i) {if (items.indexOf(items.get(i)) ! -1) {selectedItems.add(items.get(i));}}}return;}if (currentWeight items.get(level).weight capacity) {currentWeight items.get(level).weight;currentValue items.get(level).value;backtrack(level 1, currentWeight, currentValue);currentWeight - items.get(level).weight;currentValue - items.get(level).value;}if (bound(level 1, currentWeight, currentValue) maxTotalValue) {backtrack(level 1, currentWeight, currentValue);}}private static double bound(int level, int currentWeight, int currentValue) {double maxBound currentValue;int currentLevel level;int currentWeightSum currentWeight;while (currentWeightSum capacity currentLevel items.size()) {if (currentWeightSum items.get(currentLevel).weight capacity) {currentWeightSum items.get(currentLevel).weight;maxBound items.get(currentLevel).value;} else {double remainingWeight capacity - currentWeightSum;maxBound (remainingWeight / items.get(currentLevel).weight) * items.get(currentLevel).value;break;}currentLevel;}return maxBound;}private static void writeOutputData(String filename) {try {FileWriter writer new FileWriter(filename);writer.write(背包中物品的总价值: maxTotalValue);writer.write(\n\n已选物品列表:\n);for (Item item : selectedItems) {writer.write(重量: item.weight , 价值: item.value);writer.write(\n);}writer.close();System.out.println(输出成功);} catch (IOException e) {e.printStackTrace();}} }输入 运行 输出
http://www.zqtcl.cn/news/772936/

相关文章:

  • 我的专业网站建设策划书网站logo教程
  • 百度 网站 移动端win10系统之家官网
  • h5商城网站建站成都网站建设全平台
  • xuzhou公司网站制作有什么手机网站
  • 网站建设 培训深圳网站建设制作品牌公司
  • 网站到期怎么续费网站运营优化推广
  • 一站式装修的利弊上海建设厅焊工证查询网站
  • 济宁做网站公司找融合深圳招聘一般在哪个网站
  • 重庆建网站推广公司个人网站需要建站群吗
  • 深圳网站建设吗个人博客网站制作代码
  • 化妆品网站模板网络营销的网站分类有哪些
  • 广州网站建设程序员培训wordpress 微信 抓取
  • 毕设给学校做网站个人店铺logo
  • 中国做w7的网站宿迁网站建设价位
  • 网站建设售后服务合同百度关键词排名点击器
  • 编辑网站用什么软件推广是什么
  • 北京模板开发建站做网站赚钱的点在哪里
  • 网站建设价格兴田德润i网址多少wordpress主题汉化是什么意思
  • 用最少的钱做网站根据域名查询网站名称
  • 网站开发答辩难点网站返回按钮设计
  • 鹤壁做网站优化建设银行理财产品网站
  • 电子商务类网站模板自学网站建设基本流程
  • 无锡网站制作的公司上海企业服务公司
  • 做h5小程序的网站搜索引擎营销案例
  • 订餐网站开发方案查询网站是否正规
  • 建站论坛图片生成器免费
  • 怎么做自己的店铺网站博物馆门户网站建设优势
  • 专业旅游培训网站建设应用之星 wordpress
  • 青海媒体网站建设公司深圳网站建设推广优化公司
  • 网站开发 价格跨境支付互联互通