Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. // SET UP VARIABLES FOR LOOPING
  2. //
  3. // ALWAYS leave corporateHomeCity for startingCity
  4. // Add corporateHomeCity to citiesVisited
  5. citiesVisited.add(corporateHomeCity);
  6. // Add distance to startingCity
  7. ArrayList<Integer> aCityArrayList = citiesMap.get(new Integer(corporateHomeCity));
  8. totalDistanceOfTour += aCityArrayList.get(startingCity);
  9. // shrink map
  10. citiesMap.remove(new Integer(corporateHomeCity));
  11. // sets processThisCity to starting tour stop, the startingCity
  12. processThisCity = startingCity;
  13. //
  14. // THE LOOP
  15. //
  16. while( !citiesMap.isEmpty() ) {
  17. //
  18. aCityArrayList = citiesMap.get(new Integer(processThisCity));
  19. // loop set up
  20. cityClosestDistance = 0;
  21. //
  22. for(int i = 0; i < aCityArrayList.size(); i++) {
  23. if( !citiesVisited.contains(i) ) {
  24. if( processThisCity!=i ) {
  25. loopCityDistance = aCityArrayList.get(i);
  26. if (cityClosestDistance==0) {
  27. cityClosestDistance = loopCityDistance;
  28. cityClosest = i;
  29. }
  30. if (cityClosestDistance > loopCityDistance) {
  31. cityClosestDistance = loopCityDistance;
  32. cityClosest = i;
  33. }
  34. }
  35. }
  36. }
  37. //
  38. // adds closest city to list of cities that have been visited.
  39. citiesVisited.add(processThisCity);
  40. // last city get distance to starting city
  41. if( citiesMap.size()==1 ) {
  42. // adds the distance between the last city and the starting city
  43. totalDistanceOfTour += aCityArrayList.get(new Integer(corporateHomeCity));
  44. } else {
  45. // adds the distance between the currentCity and the closest city to the total distance traveled.
  46. totalDistanceOfTour += cityClosestDistance;
  47. }
  48. // shrink map
  49. citiesMap.remove(new Integer(processThisCity));
  50. // sets processThisCity to next tour stop, the cityClosest
  51. processThisCity = cityClosest;
  52. //
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement