加油站变体算法验证

Verification of algorithm for variant of gas station

我正在研究这个问题,我认为这是加油站问题的变体。结果,我使用贪心算法来解决这个问题。想请问有没有人帮我指出我的算法是否正确,谢谢。

我的算法

  var x = input.distance, cost = input.cost, c = input.travelDistance, price = [Number.POSITIVE_INFINITY];
  var result = [];

  var lastFill = 0, tempMinIndex = 0, totalCost = 0;

  for(var i=1; i<x.length; i++) {
    var d = x[i] - x[lastFill];
    if(d > c){ //car can not travel to this shop, has to decide which shop to refill in the previous possible shops
      result.push(tempMinIndex);
      lastFill = tempMinIndex;
      totalCost += price[tempMinIndex];
      tempMinIndex = i;
    }
    //calculate price
    price[i] = d/c * cost[i];
    if(price[i] <= price[tempMinIndex])
      tempMinIndex = i;
  }

  //add last station to the list and the total cost
  if(lastFill != x.length - 1){
    result.push(x.length - 1);
    totalCost += price[price.length-1];
  }

您可以在此处试用该算法 link https://drive.google.com/file/d/0B4sd8MQwTpVnMXdCRU0xZFlVRlk/view?usp=sharing

首先,关于你的解决方案。

即使是最简单的输入,也有一个错误。当您决定距离变得太远并且您应该在之前的某个时间点完成时,您不会更新距离并且加油站会向您收取更多费用。修复很简单:

if(d > c){ 
//car can not travel to this shop, has to decide which shop to refill
//in the previous possible shops
      result.push(tempMinIndex);
      lastFill = tempMinIndex;
      totalCost += price[tempMinIndex];
      tempMinIndex = i;
      // Fix: update distance
      var d = x[i] - x[lastFill];
    }

即使进行了此修复,您的算法在某些输入数据上仍会失败,如下所示:

0 10 20 30
0 20 30 50
30

它应该在每一种汽油上加满以最大限度地降低成本,但它只是在最后一种汽油上加满。

经过一番研究,我想出了解决办法。我将尝试尽可能简单地解释它以使其与语言无关。

  1. 想法

对于每个加油站G,我们将计算最便宜的加油方式。我们将递归地这样做:对于每个加油站,让我们找到我们可以到达 G 的所有加油站 i。对于每个 i 计算可能的最便宜的加注,并在给定剩余汽油的情况下加注 G 的加注成本。启动加油站的成本为 0。更正式地说:

CostOfFilling(x)CapacityPosition(x) 可以从输入数据中检索。

所以,问题的答案很简单BestCost(LastGasStation)

  1. 代码

现在,javascript 中的解决方案使事情更清楚。

function calculate(input)
{
    // Array for keeping calculated values of cheapest filling at each station
    best = [];
    var x = input.distance;
    var cost = input.cost;
    var capacity = input.travelDistance;

    // Array initialization
    best.push(0);
    for (var i = 0; i < x.length - 1; i++)
    {
        best.push(-1);
    }

    var answer = findBest(x, cost, capacity, x.length - 1);
    return answer;
}

// Implementation of BestCost function
var findBest = function(distances, costs, capacity, distanceIndex)
{
    // Return value if it's already have been calculated
    if (best[distanceIndex] != -1)
    {
        return best[distanceIndex];
    }
    // Find cheapest way to fill by iterating on every available gas station
    var minDistanceIndex = findMinDistance(capacity, distances, distanceIndex);
    var answer = findBest(distances, costs, capacity, minDistanceIndex) + 
        calculateCost(distances, costs, capacity, minDistanceIndex, distanceIndex);
    for (var i = minDistanceIndex + 1; i < distanceIndex; i++)
    {
        var newAnswer = findBest(distances, costs, capacity, i) + 
        calculateCost(distances, costs, capacity, i, distanceIndex);
        if (newAnswer < answer)
        {
            answer = newAnswer;
        }
    }
    // Save best result
    best[distanceIndex] = answer;
    return answer;
}

// Implementation of MinGasStation function
function findMinDistance(capacity, distances, distanceIndex)
{
    for (var i = 0; i < distances.length; i++)
    {
        if (distances[distanceIndex] - distances[i] <= capacity)
        {
            return i;
        }
    }
}

// Implementation of Cost function
function calculateCost(distances, costs, capacity, a, b)
{
    var distance = distances[b] - distances[a];
    return costs[b] * (distance / capacity);
}

包含代码的完整可用 html 页面可用 here