分支界限法多维背包问题

#include <iostream>  
#include <vector>  
#include <queue>  
#include <limits>  

using namespace std;

// 物品结构体  
struct Item {
    int id;         // 物品编号  
    int weight;     // 物品重量  
    int volume;     // 物品体积  
    int value;      // 物品价值  
};

// 结点结构体,用于分支限界法的搜索树  
struct Node {
    vector<int> selectedItems; // 已选择的物品编号  
    int currentValue;         // 当前总价值  
    int currentWeight;        // 当前总重量  
    int currentVolume;        // 当前总体积  
    int upperBound;           // 当前结点的上界  

    Node(const vector<int>& items, int val, int wt, int vol, int ub)
        : selectedItems(items), currentValue(val), currentWeight(wt), currentVolume(vol), upperBound(ub) {}

    // 计算上界  
    int calculateUpperBound(const vector<Item>& items, int maxWeight, int maxVolume, int depth) {
        int remainingWeight = maxWeight - currentWeight;
        int remainingVolume = maxVolume - currentVolume;
        int bound = currentValue;
        for (int i = depth; i < items.size(); ++i) {
            int count = min(items[i].weight / remainingWeight, items[i].volume / remainingVolume);
            bound += count * items[i].value;
        }
        return bound;
    }
};

// 比较函数,用于优先队列  
struct CompareNode {
    bool operator()(const Node* lhs, const Node* rhs) {
        return lhs->upperBound < rhs->upperBound;
    }
};

// 分支限界法求解多维背包问题  
int knapsack(const vector<Item>& items, int maxWeight, int maxVolume) {
    priority_queue<Node*, vector<Node*>, CompareNode> pq;
    vector<int> emptyItems;
    int bestValue = 0;
    vector<Item> selectedItems;

    // 初始化根结点  
    Node* root = new Node(emptyItems, 0, 0, 0, 0);
    root->calculateUpperBound(items, maxWeight, maxVolume, 0);
    pq.push(root);

    while (!pq.empty()) {
        Node* currentNode = pq.top();
        pq.pop();

        // 更新最优解  
        if (currentNode->currentValue > bestValue) {
            bestValue = currentNode->currentValue;
            selectedItems = {};
            for (int itemID : currentNode->selectedItems) {
                selectedItems.push_back(items[itemID - 1]);
            }
        }

        // 到达叶子结点,结束搜索  
        if (currentNode->selectedItems.size() == items.size()) {
            continue;
        }

        // 分支操作  
        for (int i = currentNode->selectedItems.size(); i < items.size(); ++i) {
            int newWeight = currentNode->currentWeight + items[i].weight;
            int newVolume = currentNode->currentVolume + items[i].volume;

            // 检查是否满足约束条件  
            if (newWeight <= maxWeight && newVolume <= maxVolume) {
                vector<int> newItems = currentNode->selectedItems;
                newItems.push_back(items[i].id);

                int newValue = currentNode->currentValue + items[i].value;
                int newUpperBound = currentNode->calculateUpperBound(items, maxWeight, maxVolume, currentNode->selectedItems.size() + 1);

                Node* newNode = new Node(newItems, newValue, newWeight, newVolume, newUpperBound);
                pq.push(newNode);
            }
        }

        // 释放当前结点内存  
        delete currentNode;
    }

    cout << "Maximum value: " << bestValue << endl;
    cout << "Selected items:" << endl;
    for (const auto& item : selectedItems) {
        cout << "Item ID: " << item.id << ", Weight: " << item.weight << ", Volume: " << item.volume << ", Value: " << item.value << endl;
    }

    return bestValue;
}

int main() {
    // 示例物品数据  
    vector<Item> items = {
        {1, 10, 20, 60},
        {2, 20, 30, 100},
        {3, 30, 40, 120},
        {4, 40, 50, 160},
        {5, 50, 60, 220}
    };
    int maxWeight = 100;
    int maxVolume = 140;
    knapsack(items, maxWeight, maxVolume);

    return 0;
}

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值