装载问题--优先队列式分支界限法

include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;

typedef struct Choice{
        Choice * pre;
        bool choose;
}Choice;

typedef struct Node{
        int up_weight;
        int level;
        Choice *ptr;
}Node;

const int n = 10;
int w[n+1] = {0};
int r[n+1] = {0};
int bestx[n+1] = {0};
int bestw = 0;
int c = 0;
vector<Node> heap;

void initParament()
{
        //TODO
}

bool compareNode(Node node1, Node node2)
{
        return node1.up_weight > node2.up_weight;
}

void insertNode(Choice *pre, int up_weight, int level, bool choose)
{
        Choice *connect = new Choice;
        connect->pre = pre;
        connect->choose = choose;

        Node node;
        node.level = level;
        node.up_weight = up_weight;
        node.ptr = connect;

        heap.push_back(node);
        push_heap(heap.begin(), heap.end(), compareNode);
}

 

 

void deleteNode(Node & node)
{
        pop_heap(heap.begin(), heap.end(), compareNode);
        node = *heap.rbegin();
        heap.erase(heap.end()-1);
}

void maxLoading()
{
        Choice *pre = NULL;
        Node node;
        int current_weight = 0;
        for(int i=n-1; i>0; i–){
                r[i] = r[i+1] + w[i+1];
        }

        int i=1;
        while(i != n+1){
                int wt = current_weight + w[i];
                if( wt < c ){
                        bestw = max(wt, bestw);
                        insertNode(pre, wt + r[i], i+1, true);
                }
                if( current_weight + r[i] > bestw ){
                        insertNode(pre, current_weight + r[i], i+1, false);
                }

                deleteNode( node );
                i = node.level;
                current_weight = node.up_weight-r[i-1];
                pre  = node.ptr;
        }

        for(i=n; i>0; i–){
                bestx[i] = pre->choose;
                pre = pre->pre;
        }
}

int main()
{
        initParament();

        maxLoading();

        return 0;
}

    原文作者:分支限界法
    原文地址: https://blog.csdn.net/hongshundi/article/details/4665498
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞