[算法]分支界限法解决装载问题

本文介绍了一种使用队列和结构体实现的最优装载问题算法。通过预设的货物重量和最大装载限制,算法能够找出最优的装载组合,使得装载的总重量最大化,同时不超过装载限制。代码详细展示了输入、处理和输出过程。
#include <bits/stdc++.h>

using namespace std;

typedef struct QNode {
    QNode *parent;// 父节点指针
    bool lChild;// 子节点
    int weight;// 货物重量
} QNode;

int n;// 货物数量
int maxLoadingWt;// 最大装载重量
int bestWt;// 最优装载量
int wt[100];// 货物重量集合
int bestCh[100];// 最优装载选择集合

void EnQueue(queue<QNode *> &q, int i, int ch, int weight, QNode *E, QNode *&bestE) {
    if (i == n) {// 达到叶子节点
        if (weight == bestWt) {// 找到最优解
            bestE = E;
            bestCh[i] = ch;
            return;
        }
    }
    QNode *tmp;
    tmp = new QNode;
    tmp->weight = weight;
    tmp->lChild = ch;
    tmp->parent = E;
    q.push(tmp);
}


void Input() {
    cout << "Please input the number of goods and the maximum of loading:" << endl;
    cin >> n >> maxLoadingWt;
    cout << "The weight array is..." << endl;
    for (int i = 1; i <= n; ++i) {
        cin >> wt[i];
    }
}

void Output() {
    cout << "->>>>>>>>>>>>>>>>>>>>>>>>" << endl;
    cout << "The maximum of loading weight is:" << bestWt << endl
         << "The choice is:" << endl;
    for (int i = 1; i <= n; ++i) {
        if (bestCh[i])
            cout << i << " ";
    }

}

void MaxLoading() {
    queue<QNode *> q;
    q.push(0);// 分层标志
    int i = 1;// 选择第一个货物
    int r = 0;// 剩余货物重量
    int wtLoaded = 0;// 当前已装货物重量
    bestWt = 0;//当前最优解
    /*计算当前剩余货物重量 已预选定第一个货物*/
    for (int j = 2; j <= n; ++j)
        r += wt[j];
    QNode *E, *bestE;
    E = new QNode;
    E = 0;// 初始父节点指针指向根(0)

    while (true) {
        int wtPreLoaded;// 预装载后货物重量
        wtPreLoaded = wtLoaded + wt[i];
        if (wtPreLoaded <= maxLoadingWt) {
            if (wtPreLoaded > bestWt)
                bestWt = wtPreLoaded;
            EnQueue(q, i, 1, wtPreLoaded, E, bestE);
        }
        if (wtLoaded + r >= bestWt)
            EnQueue(q, i, 0, wtLoaded, E, bestE);
        E = q.front();
        q.pop();
        if (!E) {
            if (q.empty())
                break;
            q.push(0);
            E = q.front();
            q.pop();
            i++;
            r -= wt[i];
        }
        wtLoaded = E->weight;
    }
    for (int j = n - 1; j > 0; --j) {
        bestCh[j] = bestE->lChild;
        bestE = bestE->parent;
    }
}

int main() {
    Input();
    MaxLoading();
    Output();
    return 0;
}
#include #include #include #include using namespace std; ifstream infile; ofstream outfile; class Node { friend int func(int*, int, int, int*); public: int ID; double weight;//物品的重量 }; bool comp1(Node a, Node b) //定义比较规则 { return a.weight > b.weight; } class Load; class bbnode; class Current { friend Load; friend struct Comp2; private: int upweight;//重量上界 int weight;//结点相应的重量 int level;//活结点在子集树中所处的层次 bbnode* ptr;//指向活结点在子集树中相应结点的指针 }; struct Comp2 { bool operator () (Current *x, Current *y) { return x->upweightupweight; } }; class Load { friend int func(int*, int, int, int*); public: int Max0(); private: priority_queue<Current*, vector, Comp2>H;//利用优先队列(最大堆)储存 int limit(int i); void AddLiveNode(int up, int cw, bool ch, int level); bbnode *P;//指向扩展结点的指针 int c;//背包的容量 int n;//物品的数目 int *w;//重量数组 int cw;//当前装载int *bestx;//最优解方案数组 }; class bbnode { friend Load; friend int func( int*, int, int, int*); bbnode* parent; bool lchild; }; //结点中有双亲指针以及左儿子标志 int Load::limit(int i) //计算结点所相应重量的上界 { int left,a; left= c - cw;//剩余容量 a = cw; //b是重量上界,初始值为已经得到的重量 while (i <= n && w[i] parent = P; b->lchild = ch; Current* N = new Current; N->upweight = up; N->weight = cw; N->level = level; N->ptr = b; H.push(N); } int Load::Max0() { int i = 1; P = 0; cw = 0; int bestw = 0; int up = limit(1); while (i != n + 1) { int wt = cw + w[i]; //检查当前扩展结点的左儿子结点 if (wt bestw) bestw =wt; AddLiveNode(up,wt, true, i + 1); } up = limit(i + 1); //检查当前扩展结点的右儿子结点 if (up >= bestw)//如果右儿子可行 { AddLiveNode(up,cw, false, i + 1); } Current* N = H.top(); //取队头元素 H.pop(); P = N->ptr; cw = N->weight; up = N->upweight; i = N->level; } bestx = new int[n + 1]; for (int j = n; j > 0; --j) { bestx[j] = P->lchild; P = P->parent; } return cw; } int func(int *w, int c, int n, int *bestx) //调用Max0函数对子集树的优先队列式进行分支限界搜索 { int W = 0; //初始化装载的总质量为0 Node* Q = new Node[n]; for (int i = 0; i < n; ++i) { Q[i].ID = i + 1; Q[i].weight = w[i+1]; W += w[i+1]; } if (W <= c)//如果足够装,全部装入 return W; sort(Q, Q + n, comp1); //首先,将各物品按照重量从大到小进行排序; Load K; K.w = new int[n + 1]; for (int j = 0; j < n; j++) K.w[j + 1] = w[Q[j].ID]; K.cw = 0; K.c = c; K.n = n; int bestp = K.Max0(); for (int k = 0; k < n; k++) { bestx[Q[k].ID] = K.bestx[k + 1]; } delete []Q; delete []K.w; delete []K.bestx; return bestp; } int main() { int*w,*Final; int c,n,i,best; infile.open("input.txt",ios::in); if(!infile) { cerr<<"open error"<>c; infile>>n; w=new int[n+1]; for(i=1;i>w[i]; infile.close(); Final = new int[n+1]; best = func( w, c, n, Final); outfile.open("output.txt",ios::out); if(!outfile) { cerr<<"open error"<<endl; exit(1); } outfile << best << endl; for (int i = 1; i <= n; ++i) { outfile<<Final[i]<<" "; } outfile.close(); return 0; }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值