The amount of money in each city is unknown, so the best possible route is the shortest route from one city to another.
Here is what I would like to do if I programmed it in Javascript using recursion:
http://jsfiddle.net/rd13/ShL9X/5/
c = ['london', 'manchester', 'liverpool']; //Cities t = { 'liverpool': { 'manchester': 130, 'london': 260 }, 'london': { 'manchester': 250, 'liverpool': 260 }, 'manchester': { 'london': 250, 'liverpool': 130 } } //Time between cities cn = 'london'; //Current city var shortestDistance = 500, allottedTime = 300, next_city, time = 0, totalTime = 0, path = [cn]; optimal = function (cn) { for (i in t[cn]) { //So as not to visit cities that we have already visited if(path.indexOf(i)===-1) { next_city = t[cn][i] < shortestDistance ? i : next_city; shortestDistance = t[cn][i]; totalTime = Math.min(t[cn][i] , shortestDistance); } } time += totalTime; path.push(next_city); if(time > allottedTime){ document.write("The most optimal route between cities is: " + path); } else { optimal(next_city); } } optimal(cn);
Sorry, there is no help on the algorithm, this is from a programming point of view, since I thought it was an interesting task. The above is not an optimal mind.
source share