TSP(分支限界法)

#include<bits/stdc++.h>
#define INF 60000
#define n 4
using namespace std;
int** a;
int  bestc = INF;
int* bestx;
int* minout;
int sum_minout;
class Node{
public:
    int t_;
    int ccost_;
    int least_cost_;
    int* x_;
public:
    void Init_Node(Node node ,int i);//根据父亲节点生成子节点
    void swap(int t_,int i){
       int y  = x_[t_];
       x_[t_] = x_[i];
       x_[i] = y;       }
    bool Judge();
public:
    bool operator < (const Node &a) const
    {
        return least_cost_ > a.least_cost_;
    }
    friend void Branch_bound();//分支限界法
};
void Node::Init_Node(Node node ,int i){//根据父亲节点构造子节点
    t_ = node.t_+ 1;
    x_ = new int [n];
    for(int i=0;i<n;i++)
        x_[i] = node.x_[i];
    swap(t_, i);
    ccost_ = node.ccost_ + a[x_[t_]][x_[t_- 1]];
    least_cost_ = ccost_;
    for(int i=t_;i<n;i++){
        least_cost_ += minout[i];
    }
}
bool Node::Judge(){   //约束函数和限界函数
    if(t_==n-1){      //叶子节点
        if(a[x_[t_- 1]][x_[t_]]!=INF &&a[x_[t_]][x_[0]]!=INF
        &&ccost_ + a[x_[t_]][x_[0]]< bestc){
              ccost_ += a[x_[t_]][x_[0]];
              return true;
        }
    }
    else {           //非叶子节点
        if(a[x_[t_]][x_[t_- 1]]!=INF)
            if(least_cost_ < bestc)
             return true;
    }
      return false;
}
void Init(){
    a = new int*[n + 1];
    for(int i=1;i<=n;i++){
       a[i] = new int [n+1];
    }
    //初始化邻接矩阵
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            a[i][j] = INF;
        }
    }
    //输入边和相应的权重
    cout<<"input number of edges"<<endl;
    int edgenum;
    cin>>edgenum;
    cout<<"input start end and weight"<<endl;
    for(int i=0;i<edgenum;i++){
        int s,e,w;
        cin>>s>>e>>w;
        a[s][e] = w;
        a[e][s] = w;
    }
    //初始化minout数组
    minout = new int[n];
    for(int i=1;i<=n;i++){
            int min_ = INF;
        for(int j=1;j<=n;j++){
            if(a[i][j]<min_)
              min_ = a[i][j];
      }
      minout[i-1] = min_;
      sum_minout += min_;
    }
}
priority_queue<Node> que;
void Branch_bound(){
    //初始化,根节点入队
    Node* a;
    a = new Node;
    a->t_ = 0;
    a->ccost_ = 0;
    a->least_cost_ = sum_minout;
    a->x_ = new int [n];
    for(int i=0;i<n;i++)
        a->x_[i] = i + 1;
    que.push(*a);
    //
    while(!que.empty()){           //队列不为空的话
        Node* node;
      for(int i = que.top().t_+ 1;i<n;i++){
           node = new Node;
           node->Init_Node(que.top(),i);
           if(node->Judge())
             que.push(*node);
           else
            delete node->x_;
        }
        if(que.top().t_==n-1){       //根节点位于优先队列头则直接更新最优值,结束循环
            //if(que.top().ccost_ < bestc){//}
            bestc = que.top().ccost_;
            bestx = que.top().x_;
            break;
        }
        que.pop();
    }
}
void print(){
    cout<<"bestc : "<<bestc<<endl;
    cout<<"The path is"<<endl;
    for(int i=0;i<n;i++){
        cout<<bestx[i]<<" ";
    }
}
int main(){
     Init();
     Branch_bound();
     print();
     return 0;
}
//6
//1 2 30
//1 3 6
//1 4 4
//2 3 5
//2 4 10
//3 4 20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值