*1.问题描述:*
*2.解题思路*
这个题目基本思想是 利用分支界限法, 核心就是需要设计一个 优先级标准, 这里我们将 问题的层数,也就是第i个部件作为优先级, 对于相同i的部件,以重量更小的作为优先级的评价标准,然后借助标准库中的优先级队列实现,分支界限法 查找目标。
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
原文地址:http://blog.csdn.net/zhyh1435589631/article/details/45977631