C + + implementation scheme of branch boundary method for minimum weight problem

Source: Internet
Author: User
Tags readfile

* * Description of the problem: *

* * * problem solving ideas *
The basic idea of this topic is to use the branch boundary method, the core is the need to design a priority standard, here we will be the problem layer, that is, the first part as the priority, for the same part of I, with a smaller weight as a priority evaluation criteria, and then with the standard library in the priority queue implementation, branch boundary method to find the target

It is also important to note that using the priority queue in the standard library requires you to overload the operator< and must have const,233333333

3. Source Code
3.1minmachine.h

#pragma once#include <queue>typedefstruct_node {_node * parent;intTotal_price;intTotal_weight;intPriorityintsupplier_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    //Priority setting, by layer, the larger the layer    //The higher the priority, the lighter the weight in the same layer, the higher the priority level    //************************************    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();voidWriteFile ();voidSearchres ();voidOutput (Node * curnode);BOOLIsOK (intCur_price);Private:intM_m;intM_n;intM_d;int* * M_C;int* * M_W; std::p riority_queue<node> machheap;intM_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(inti =0; I! = m_n; i++) {if(M_c[i])DeleteM_c[i];if(M_w[i])DeleteM_w[i]; }if(M_c)Delete[] m_c;if(M_W)Delete[] m_w;if(m_select_best)DeleteM_select_best;}voidCminmachine::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(inti =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(intj =0; J! = M_n;        J + +) {infile >> m_c[i][j]; }    } for(inti =0; I! = m_n; i++) { for(intj =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));}voidCminmachine::writefile () {STD:: Ofstream outfile ("Output.txt"); outfile << m_best <<STD:: Endl; for(inti =0; I! = m_n; i++) {outfile << m_select_best[i] +1<<"\ T"; } outfile <<STD:: Endl;}intCminmachine::solve () {ReadFile ();    Searchres (); WriteFile ();returnM_best;}//************************************//Method:searchres//Fullname:cminmachine::searchres//Access:private//Returns:void//Qualifier: After the priority has been designed, element nodes are queued according to boundary conditions//************************************voidCminmachine::searchres () {Node root; Machheap.push (root); while(true) {Node * Curnode =NewNode (Machheap.top ()); Machheap.pop ();if(curnode->priority = = M_n-1) {output (Curnode);return; } for(inti =0; I! = m_m; i++) {Node * subnode =NewNode;            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); }    }}voidCminmachine::output (Node * curnode) {m_best = curnode->total_weight; for(inti = m_n-1; I! =-1;        i--) {M_select_best[i] = curnode->supplier_id;    Curnode = curnode->parent; }}BOOLCminmachine::isok (intCur_price) {returnCur_price <= M_d;}

3.3.main.cpp

//-------------------------"CHP 6 min Machine weight"----------------------//@ author:zhyh2010//@ date:20150524//@ version:1.0//@ Description: With priority queue implementation//@ idea: Branch boundaries sent//-----------------------------------"End Tip"-------------------------------#include "MinMachine.h"#include <iostream>voidMain () {Cminmachine instance;intres = Instance.solve ();STD::cout<<"res ="<< Res <<STD:: Endl;}

4. Reference Content
Minimum weight machine design problem 5_1 6_4

STL Artifact, priority queue

C + + implementation scheme of branch boundary method for minimum weight problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.