Guest User

Untitled

a guest
Nov 20th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.80 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Q2</title>
  6. </head>
  7. <body>
  8. <input type="text" id="distance" style="width: 50%" placeholder="Distance from this station to source, start with 0, split by space"/><br/>
  9. <input type="text" id="cost" style="width: 50%" placeholder="Cost of each station, start with 0, split by space"/><br/>
  10. <input type="text" id="travel" style="width: 50%" placeholder="Maximum travel distance" /><br/>
  11. <button onclick="solve()">Solve</button>
  12. <div id="result"></div>
  13.  
  14. <script>
  15.     var best;
  16.     function solve()
  17.     {
  18.         var input = getInput();
  19.         var result = calculate(input);
  20.         showResult(input, result);
  21.     }
  22.  
  23.     function getInput ()
  24.     {
  25.         var distanceText = document.querySelector("#distance").value;
  26.         var costText = document.querySelector("#cost").value;
  27.         var travelDistance = Number(document.querySelector("#travel").value);
  28.         var distanceArr = distanceText.split(" ").map(function(text){return Number(text);});
  29.         var costArr = costText.split(" ").map(function(text){return Number(text);});
  30.         return {
  31.             distance: distanceArr,
  32.             cost: costArr,
  33.             travelDistance: travelDistance
  34.         };
  35.     }
  36.  
  37.     function calculate(input)
  38.     {
  39.         best = [];
  40.         var x = input.distance;
  41.         var cost = input.cost;
  42.         var capacity = input.travelDistance;
  43.  
  44.         best.push(0);
  45.         for (var i = 0; i < x.length - 1; i++)
  46.         {
  47.             best.push(-1);
  48.         }
  49.  
  50.         var answer = findBest(x, cost, capacity, x.length - 1);
  51.         return answer;
  52.     }
  53.  
  54.     // Implementation of BestCost function
  55.     var findBest = function(distances, costs, capacity, distanceIndex)
  56.     {
  57.         if (best[distanceIndex] != -1)
  58.         {
  59.             return best[distanceIndex];
  60.         }
  61.         var minDistanceIndex = findMinDistance(capacity, distances, distanceIndex);
  62.         var answer = findBest(distances, costs, capacity, minDistanceIndex) +
  63.             calculateCost(distances, costs, capacity, minDistanceIndex, distanceIndex);
  64.         for (var i = minDistanceIndex + 1; i < distanceIndex; i++)
  65.         {
  66.             var newAnswer = findBest(distances, costs, capacity, i) +
  67.             calculateCost(distances, costs, capacity, i, distanceIndex);
  68.             if (newAnswer < answer)
  69.             {
  70.                 answer = newAnswer;
  71.             }
  72.         }
  73.         best[distanceIndex] = answer;
  74.         return answer;
  75.     }
  76.  
  77.     // Implementation of MinGasStation function
  78.     function findMinDistance(capacity, distances, distanceIndex)
  79.     {
  80.         for (var i = 0; i < distances.length; i++)
  81.         {
  82.             if (distances[distanceIndex] - distances[i] <= capacity)
  83.             {
  84.                 return i;
  85.             }
  86.         }
  87.     }
  88.  
  89.     // Implementation of Cost function
  90.     function calculateCost(distances, costs, capacity, a, b)
  91.     {
  92.         var distance = distances[b] - distances[a];
  93.         return costs[b] * (distance / capacity);
  94.     }
  95.  
  96.     function showResult(input, result)
  97.     {
  98.         var resultText = "Total cost is "+ result;
  99.         document.querySelector("#result").innerHTML = resultText;
  100.     }
  101. </script>
  102. </body>
  103. </html>
Advertisement
Add Comment
Please, Sign In to add comment