Problem Description:
The problem of traveling merchant (traveling salesman problem,tsp) is that the traveller travels to several cities and the cost between the cities is known and, in order to save costs, the traveller decides to travel from the city to the city and return to the original city once a trip has taken place, Ask him what kind of route he should choose to make the total cost of his travel the shortest. This problem can be described as follows: Set g= (v,e) is a direction graph with edge cost CIJ, CIJ is defined as follows, for all I and j,cij>0, if <i,j> does not belong to E, then cij=∞. Order | V|=n, and assume n>1. A travel route for G is a forward loop that contains each node in V, and the cost of traveling the route is the cost of all the edges on this route.
Problem Analysis:
The traveling business problem is to get the lowest cost travel route from all the travel routes of Figure G, while the travel route from the initial point is altogether (n-1)! is equal to the number of N-1 nodes except the initial node, so the traveling merchant problem is an arrangement problem. The problem of permutation is usually more difficult to solve than the selection of a n!, because N objects have a sort of arrangement, and only a child set (N!>o ()). Through the enumeration (N-1)! A travel route is used to find a routing algorithm with the lowest cost, which obviously has an O (n!) calculation time. The code is as follows (there's still a problem, but I'll change it later):
#include <stdio.h> #include <stdlib.h> #include <memory.h> #define MAX 1000 int* X = NULL;
BOOL NextValue (int k) {int i = 1;
while (i<k) {if (x[i] = = X[k]) return false;
i++;
return true;
} void Travelerproblem (int** w, int n) {int k;
int * TEMP = (int *) malloc ((n+1) *sizeof (int));
X = temp;
int CL,FL;
for (int i=1;i<=n;i++) x[i] = 1; k=2; cl=0;
Fl=max;
while (k>1) {x[k] = (x[k]+1)%n;
for (Int J =0; j<n;j++)//Allocation state space tree, which node {if (NextValue (k) = = true) {cl = cl + w[x[k-1]][x[k]];
Break
} X[k] = (x[k]+1)%n; } if ((fl<= cl) | | (K==n && fl<cl+w[x[k]][1])
If the node you choose now has already made FL<=CL or has all been selected but is still longer than the previous shortest path {cl-=w[x[k-1]][x[k]];
K-=1;
else if (k==n && fl>= cl+w[x[k]][1]) {FL = cl + w[x[k]][1];
K-=1;
else k+=1;
} void Main () {puts ("Please input the Counts of vertex:");
int n = 0;
scanf ("%d", &n); Puts ("Please input" the distance of two vertex: (1Mean it is no edge between two Vetex.) ");
int** w = (int * *) malloc ((n+1) *sizeof (int *));
for (int i=1;i<=n;i++) {W[i] = (int *) malloc ((n+1) *sizeof (int));
memset (W[i], 0, (n+1) *sizeof (int));
for (int i=1;i<=n;i++) {for (int j=1; j<=n;j++) {printf ("The%dth row,%dth column value is:", I, j);
scanf ("%d", &w[i][j]);
} travelerproblem (w, N);
return; }
Problem:
1, know how to define the dynamic two-dimensional array;
2, the idea of this algorithm is still not quite understand;
3, backtracking method Ah backtracking method, do not understand AH ~ ~ ~