【算法】分枝定界

在这里插入图片描述

~

关于 构造最优解部分,有待改进(传个vector,我真离谱)。
关于 优先 什么,我有点不太确定。这个是他的 下界(很虚,但是我似乎找不到更高的下界了)

#include <iostream>
#include <queue>
#include<algorithm>
#include<vector>

using namespace std;

const int maxn = 1e3;
const int inf =1e9;
int c[maxn][maxn], w[maxn][maxn];
vector<int> ans;
int n, m, d;
int bestW=inf;
struct Node
{
    int level, currW, currC;
    vector<int> tmp;
    Node(int lev, int w, int c,vector<int> tmp_fun) : level(lev), currW(w), currC(c),tmp(tmp_fun){};
};

struct func
{
    bool operator()(Node & node1,Node & node2)
    {
        return node1.currW>node2.currW;
    }
};

void branch_bound()
{
    priority_queue<Node,vector<Node>,func> q;
    Node init_node{-1, 0, 0,vector<int>()};
    q.push(init_node);

    while (!q.empty())
    {
        auto node = q.top();
        q.pop();

        int lev = node.level, currW = node.currW, currC = node.currC;
        vector<int> tmp=node.tmp;
        
        if (lev == n - 1)
        {
            bestW=currW;
            ans=tmp;
            return ;
        }
        else
        {

            for (int i = 0; i < m; ++i)
            {
                tmp.push_back(i);
                if (currC + c[lev + 1][i] <= d)
                    q.push(Node{lev + 1, currW + w[lev + 1][i], currC + c[lev + 1][i],tmp});
                tmp.pop_back();
            }
        }
    }
}

int main()
{
    cin >> n >> m >> d;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            cin >> c[i][j];
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            cin >> w[i][j];

    branch_bound();
    
    //输出部分
    if(bestW!=inf)
    {
        cout<<bestW<<endl;
        for(auto x:ans) cout<<x+1<<" ";
    }
    else 
    {
        cout<<"No Solution!";
    }
    cout<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值