/*
* @file TSP.cpp
* @brief solve TSP with Backtrack's way
* @author/Univ. taoxiaoxiao/XMU
* @date 12-2-2013
*/
//回溯法求解TSP
#include <iostream>
#include <fstream>
using namespace std;
#define MAXN 10
#define INF 0x3f3f3f3f
int x[MAXN + 1], bestx[MAXN + 1];
int graph[MAXN+1][MAXN+1];
int cw=0, bestw=INF;
int n;
void output()
{
cout << bestw << endl;
for (int i = 1; i <= n; ++i)
cout << bestx[i] << " ";
cout << x[1] << endl;
cout << "********************" << endl;
}
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void BacktrackTSP(int t)
{
if (t == n)
{
if (graph[x[n - 1]][x[n]] != INF && graph[x[n]][1] != INF)
{
if (cw + graph[x[n - 1]][x[n]] + graph[x[n]][1] < bestw)
{
bestw = cw + graph[x[
回溯法求解TSP问题
最新推荐文章于 2024-07-21 15:07:10 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)