TSP(分支限界法)

#include
#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

你可能感兴趣的:(算法设计,c++)