/* * This program attempts to find a good tour for the 3D Euclidean * Travelling Salesman Problem. The input data file begins with an * integer n, the number of points to visit. On the next n lines * are x, y, z coordinates that are read into an array of points. * * If n <= 12, then the optimal solution is computed by trying all * (n-1)! permutations. Otherwise, a number of random tours are * tried and the best one chosen. In either case, when a new best value * is found, it is printed to standard error to keep the user entertained. * * At the end of the program, the best tour is printed on standard output * as a sequence of indices. Index 0 is always printed first; it is * assumed that this is where the saleman starts his tour. */ #include #include /* point structure: three floats */ typedef struct _point { float x, y, z; } point; /* Euclidean distance in 3-space. Find the distance between vertices i and j */ float distance (point v[], int i, int j) { float dx, dy, dz; dx = v[i].x - v[j].x; dy = v[i].y - v[j].y; dz = v[i].z - v[j].z; return sqrt (dx*dx + dy*dy + dz*dz); } /* compute the length of a tour, starting from and ending at the first * index in the tour */ float tour_length (int tour[], point v[], int n) { int i, from, to; float sum; /* sum up distances between cities on the tour */ sum = 0; /* start at city 0 */ from = 0; for (i=1; i