We are working on the travelling salesman problem in c++ for school, but we cannot figure out how to read the file we have to implement.
This is our txt file:
5
2 4 8 3
4 1 3
7 3
9
Where 5 is the number of cities and 2 is the distance between city 0 and 1, 4 between 0 and 2, 8 between 0 and 3 and 3 between 0 and 4 etcetera. We would like to make it a matrix like this:
0 2 4 8 3
2 0 4 1 3
4 4 0 7 3
8 1 7 0 9
3 3 3 9 0
We're working on it for hours now and we still can't figure out how to read this data from the file.. Could someone help us?
When you say 'we would like to make a matrix like this'. What do the values in the second matrix represent? Can you perhaps explain the question better / in a different way? It might not be your fault... I'm feeling a bit slow today, but I do believe I can help you, if I can only know what answer you're looking for... lol
The second matrix represents the distances between the cities:
0 2 4 8 3
2 0 4 1 3
4 4 0 7 3
8 1 7 0 9
3 3 3 9 0
Where the first row are the distances between city 0 and city 0 (which of course is zero), 1,2,3 and 4 (there are 5 cities, city 0, 1,2,3 and 4) .
Then the second row are the distances between city 1 and city 0,1,2,3 and 4
etc.
And we have to generate this matrix from the txt file:
5
2 4 8 3
4 1 3
7 3
9
Where the number 5 represents the total number of cities and the other numbers are numbers in the second matrix, which I made bold.
Btw we have to do this for different numbers of cities, so it should be a general program for n cities..