最小重量问题的分支界限法的C++实现方案
2015-05-25 21:09
288 查看
*1.问题描述:*
*2.解题思路*
这个题目基本思想是 利用分支界限法, 核心就是需要设计一个 优先级标准, 这里我们将 问题的层数,也就是第i个部件作为优先级, 对于相同i的部件,以重量更小的作为优先级的评价标准,然后借助标准库中的优先级队列实现,分支界限法 查找目标。
3.1MinMachine.h
3.2.MinMachine.cpp
3.3.Main.cpp
4.参考内容
最小重量机器设计问题 5_1 6_4
STL 的神器,优先队列
*2.解题思路*
这个题目基本思想是 利用分支界限法, 核心就是需要设计一个 优先级标准, 这里我们将 问题的层数,也就是第i个部件作为优先级, 对于相同i的部件,以重量更小的作为优先级的评价标准,然后借助标准库中的优先级队列实现,分支界限法 查找目标。
另外需要注意的是, 使用标准库中的优先级队列时候需要自己重载operator< ,而且一定要有const,233333333
3.源代码3.1MinMachine.h
#pragma once #include <queue> typedef struct _Node { _Node * parent; int total_price; int total_weight; int priority; int supplier_id; _Node() { parent = nullptr; total_weight = 0; total_price = 0; priority = 0; supplier_id = 0; } //************************************ // Method: operator< // FullName: _Node::operator< // Access: public // Returns: bool // Qualifier: const // Parameter: const _Node & a // 优先级 设定 ,按照 层, 层越大 // 优先级越高, 同层中 重量越轻, 优先级越高 //************************************ bool operator< (const _Node & a) const { if (priority < a.priority) return true; if (priority == a.priority) if (total_weight <= a.total_weight) return true; return false; } }Node; class CMinMachine { public: CMinMachine(); ~CMinMachine(); public: int solve(); private: void ReadFile(); void WriteFile(); void SearchRes(); void output(Node * curNode); bool IsOK(int cur_price); private: int m_m; int m_n; int m_d; int ** m_c; int ** m_w; std::priority_queue<Node> MachHeap; int m_best; int * m_select_best; };
3.2.MinMachine.cpp
#include "MinMachine.h" #include <fstream> CMinMachine::CMinMachine() { m_m = 0; m_n = 0; m_d = 0; m_c = NULL; m_w = NULL; m_best = 0; m_select_best = nullptr; } CMinMachine::~CMinMachine() { for (int i = 0; i != m_n; i++) { if (m_c[i]) delete m_c[i]; if (m_w[i]) delete m_w[i]; } if (m_c) delete[] m_c; if (m_w) delete[] m_w; if (m_select_best) delete m_select_best; } void CMinMachine::ReadFile() { std::ifstream infile("input.txt"); infile >> m_n >> m_m >> m_d; m_c = new int *[m_n]; m_w = new int *[m_n]; for (int i = 0; i != m_n; i++) { m_c[i] = new int[m_n]; m_w[i] = new int[m_n]; memset(m_c[i], 0, sizeof(m_c[i])); memset(m_w[i], 0, sizeof(m_w[i])); for (int j = 0; j != m_n; j++) { infile >> m_c[i][j]; } } for (int i = 0; i != m_n; i++) { for (int j = 0; j != m_n; j++) { infile >> m_w[i][j]; } } m_select_best = new int[m_n]; memset(m_select_best, 0, sizeof(m_select_best)); } void CMinMachine::WriteFile() { std::ofstream outfile("output.txt"); outfile << m_best << std::endl; for (int i = 0; i != m_n; i++) { outfile << m_select_best[i] + 1 << "\t"; } outfile << std::endl; } int CMinMachine::solve() { ReadFile(); SearchRes(); WriteFile(); return m_best; } //************************************ // Method: SearchRes // FullName: CMinMachine::SearchRes // Access: private // Returns: void // Qualifier: 在设计了优先级之后,根据边界条件 将元素节点 入队列 //************************************ void CMinMachine::SearchRes() { Node root; MachHeap.push(root); while (true) { Node * curNode = new Node(MachHeap.top()); MachHeap.pop(); if (curNode->priority == m_n - 1) { output(curNode); return; } for (int i = 0; i != m_m; i++) { Node * subNode = new Node; subNode->parent = curNode; subNode->total_price = curNode->total_price + m_c[curNode->priority][i]; subNode->total_weight = curNode->total_weight + m_w[curNode->priority][i]; subNode->priority = curNode->priority + 1; subNode->supplier_id = i; if (IsOK(subNode->total_price)) MachHeap.push(*subNode); } } } void CMinMachine::output(Node * curNode) { m_best = curNode->total_weight; for (int i = m_n - 1; i != -1; i--) { m_select_best[i] = curNode->supplier_id; curNode = curNode->parent; } } bool CMinMachine::IsOK(int cur_price) { return cur_price <= m_d; }
3.3.Main.cpp
// -------------------------【chp 6 最小机器重量】---------------------- // @ author : zhyh2010 // @ date : 20150524 // @ version : 1.0 // @ description : 借助优先级队列实现 // @ idea : 分支界限发 // -----------------------------------【end tip】------------------------------- #include "MinMachine.h" #include <iostream> void main() { CMinMachine instance; int res = instance.solve(); std::cout << "res = " << res << std::endl; }
4.参考内容
最小重量机器设计问题 5_1 6_4
STL 的神器,优先队列
相关文章推荐
- 最小重量机器设计问题 java实现
- 矩阵链乘法问题(给A1A2A3...An加括号,使之乘法次数最小)动态规划问题C++实现
- 【C++基本功补习】查找三个数据中的最大值、最小值、中间值,两种实现方案比较
- 图着色问题 配色方案 C++实现 回溯法
- 拉丁矩阵问题 利用回溯法的C++实现方案
- 模拟退火算法求函数最小值问题的C/C++实现
- 矩阵链乘法问题(给A1A2A3...An加括号,使之乘法次数最小)动态规划问题C++实现
- C++利用vector容器实现最大最小元问题
- C++实现Prim与Kruskal最小生成树算法
- 二维带权邮局位置(选址)问题(分别求横坐标、纵坐标的带权中位数)C++实现
- ACM John的农场(最小生成树) C++实现
- 流水线调度最优问题(装配线调度问题)动态规划 O(n)时间(线性时间)C++实现
- ACM最长公共子序列问题(动态规划)C++实现
- ACM最长单调递增子序列问题(动态规划)o(n*n)C++实现
- 活动选择问题(活动安排问题)(最大数目活动选择问题)贪心算法C++实现
- 区间图着色问题(贪心算法)C++实现
- 最小生成树Kruskal算法实现C++实现
- 最小生成树Prim算法实现(采用邻接表存储)C++实现
- ACM唯一的最小生成树C++实现
- 问题六十八:Phong反射模型的C++实现