The Traveling Salesman problem is a famous problem in Computer Science. In this question, you will write some code that would be used in solving this problem. (You won't have to write a program to solve the whole problem!) The complete Traveling Salesman Problem can be stated as:
Given: a set of cities that salesman must visit, and the distances between each pair of cities.
Find: the order that the salesman should visit the cities so as to have him travel the least distance.
If there are N cities, the cities are numbered from 1 to N.
The distance between cities is stored in a two-dimensional
array, D, where
is the distance between cities i and j.
You are to write a Turing program that inputs the 2-D distance array, and the order of cities that the salesman visits, from a single file. Your program should prompt the user for the name of the file. The program should simply calculate the distance that the salesman would travel if he visited the cities in that order.
Use the following format for the input file:
N D11 D21 D31 .. DN1 D12 D22 D32 .. DN2 D13 D23 D33 .. DN3 . . D1N D2N D3N .. DNN C1 C2 C3 C4 .. CN
Here N is the number of cities, the is the distance between city
i and city j, and the list C1, C2, .. CN gives the order of the cities
visited.
For example, if the distance matrix is given as:
And the order of cities visited was 4 3 6 1 5 2, then the
output would be calculated as:
+
+
+
+
= 6 + 2 + 66 + 33 + 952 = 1059.
The output of your
program should simply be:
For the city order 4 3 6 1 5 2 the distance traveled is 1059.