Problem Description:
A salesman to a number of cities to sell goods, known to the distance between cities, he should select a from the station, through each city again, the last return to the route, so that the total distance shortest.
Algorithm Description:
Backtracking method, sequence tree, assuming the starting point is 1.
At the start of the algorithm x = [1, 2, 3, ..., n]
X[1:N] has a double meaning x[1:i] represents a city in order of the previous I step, X[i + 1:n] represents a city that has not yet passed. Swap the position using the swap function.
If the level I = N of the current search is on the parent node of the leaf node of the arrangement tree, then the algorithm checks that there is an edge from the vertex x[n-1] to the vertex x[n, and that there is an edge from the vertex x[n to the vertex x[1]. If these two sides exist, then found a travel salesman's loop namely: new travel routes, the algorithm to determine whether the cost of this circuit is better than the current optimal circuit has been found in the cost bestcost, if, then update the current optimal bestcost and the current optimal solution bestx.
If I < n, check x[i-1] to x[i] whether there is a side, if there is, then X [1:i] forms a path of graph G, if the cost of path X[1:I] is less than the current optimal solution, then the algorithm goes into the next layer of the arrangement tree, otherwise the corresponding subtree is cut off.
Code implementation:
#include <bits/stdc++.h>
using namespace Std;
const int max_ = 0X3F3F3F;
const int Noedge =-1;
int citynum;
int edgenum;
int currentcost;
int bestcost;
int graph[100][100];
int x[100];
int bestx[100];
void InPut ()
{
int pos1, Pos2, Len;
scanf ("%d%d", &citynum, &edgenum);
memset (graph, Noedge, sizeof (graph));
for (int i = 1; I <= edgenum; ++i)
{
scanf ("%d%d%d", &POS1, &pos2, &len);
GRAPH[POS1][POS2] = graph[pos2][pos1] = len;
}
}
void Initilize ()
{
Currentcost = 0;
Bestcost = Max_;
for (int i = 1; I <= citynum; ++i)
{
X[i] = i;
}
}
void Swap (int &a, int &b)
{
int temp;
temp = A;
A = b;
b = temp;
}
void BackTrack (int i)//Here I represent the city of step I to go instead of the city of the Code No.
{
if (i = = Citynum)
{
if (Graph[x[i-1]][x[i]]!= noedge && graph[x[i]][x[1]]!= noedge && (Currentcost + graph[x[i-1]][x[i]] + graph[x[i]][x[1]] < Bestcost | | Bestcost = = max_))
{
Bestcost = Currentcost + graph[x[i-1]][x[i]] + graph[x[i]][x[1];
for (int j = 1; j <= Citynum; ++j)
BESTX[J] = X[j];
}
}
Else
{
for (int j = i; j <= Citynum; ++j)
{
if (Graph[x[i-1]][x[j]]!= noedge && (Currentcost + graph[x[i-1]][x[j]] < Bestcost | | bestcost = max_))
{
Swap (X[i], x[j]); Here I and J of the position exchange, so the following is Currentcost + + graph[x[i-1]][x[i]];
Currentcost + = Graph[x[i-1]][x[i]];
BackTrack (i + 1);
Currentcost-= graph[x[i-1]][x[i]];
Swap (X[i], x[j]);
}
}
}
}
void OutPut ()
{
cout << "route is:" << Endl;
for (int i = 1; I <= citynum; ++i)
cout << Bestx[i] << "";
cout << "1" << Endl;
}
int main ()
{
InPut ();
Initilize ();
BackTrack (2);
OutPut ();
}
Test Sample:
Input:
4 6
1 2 30
1 3 6
1 4 4
2 4 10
2 3 5
3 4 20
Output:
The course is:
1 3 2 4 1
Screenshots: