我正在研究这个问题,我认为这是加油站问题的一个变种。因此,我使用贪婪算法来解决这个问题我想问问有没有人帮我指出我的算法是正确的,谢谢。
我的算法
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];
}
你可以在这个链接上试试算法
https://drive.google.com/file/d/0B4sd8MQwTpVnMXdCRU0xZFlVRlk/view?usp=sharing
最佳答案
首先,关于你的解决方案。
即使是最简单的输入也会有一个bug当你觉得距离太远,你应该在某个点之前完成,你不更新距离和加油站收费你更应该。解决方法很简单:
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
它应该在每一种汽油上加油,以尽量降低成本,但它只是在最后一种汽油上加油。
经过一番研究,我想出了解决办法。我会尽可能简单地解释它,使它独立于语言。
想法
对于每个加油站,我们都会计算最便宜的加油方式。我们将递归地这样做:对于每个加油站,让我们找到所有可以到达的加油站。对于每一个
G
计算可能的最便宜的加油量,并在给定汽油剩余量的i
处加总加油成本。因为启动加油站的费用是0更正式地说:G
、i
和G
可以从输入数据中检索。所以,问题的答案是
CostOfFilling(x)
代码
现在,用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