Algorithm 加油站变型算法的验证
我正在研究这个问题,我认识到这是加油站问题的一个变种。因此,我使用贪婪算法来解决这个问题。我想问是否有人帮我指出我的算法是否正确,谢谢 我的算法Algorithm 加油站变型算法的验证,algorithm,dynamic-programming,greedy,Algorithm,Dynamic Programming,Greedy,我正在研究这个问题,我认识到这是加油站问题的一个变种。因此,我使用贪婪算法来解决这个问题。我想问是否有人帮我指出我的算法是否正确,谢谢 我的算法 var x = input.distance, cost = input.cost, c = input.travelDistance, price = [Number.POSITIVE_INFINITY]; var result = []; var lastFill = 0, tempMinIndex = 0, totalCost =
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];
}
var x=input.distance,cost=input.cost,c=input.travelDistance,price=[Number.POSITIVE_无穷大];
var结果=[];
var lastFill=0,tempMinIndex=0,totalCost=0;
对于(var i=1;ic){//汽车无法行驶到此商店,必须在以前可能的商店中决定要重新加注的商店
结果:推送(tempMinIndex);
lastFill=tempMinIndex;
总成本+=价格[tempMinIndex];
tempMinIndex=i;
}
//计算价格
价格[i]=付款交单*成本[i];
如果(价格[i]首先,关于您的解决方案
存在一个错误,即使是最简单的输入也会破坏。当您决定距离太远,并且您应该在某个时间点之前完成时,您不会更新距离,加油站会向您收取超出其应收取的费用。解决方法很简单:
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
我们将计算最便宜的加油方式。我们将递归地这样做:对于每个加油站,让我们找到所有可以到达G
的加油站i
。对于每个i
计算可能最便宜的加油方式,并将给定汽油剩余的G
加油成本相加。对于启动加油站成本为0。更正式地说:
CostOfFilling(x)
,容量
和位置(x)
可以从输入数据中检索
因此,问题的答案就是BestCost(lastgastation)
代码
现在,使用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);
}
函数计算(输入)
{
//用于保存每个站点最便宜填充物的计算值的数组
最佳=[];
var x=输入距离;
var成本=投入成本;
var容量=输入。行程距离;
//数组初始化
最佳。推(0);
对于(变量i=0;i 如果(距离[distanceIndex]-距离[i]对不起,但是我发现您的解决方案有一个计数器测试用例,如果我使用[01325647495263100],[0564646139813]和40.您的解决方案给出了56.62499999,而最低成本是53.525,您想检查一下这有什么问题吗?@user2534365您能提供解决方案(正确的加油站)吗还有一件事,我想问一下,如果我想知道哪些站点需要停止,我需要添加什么代码?@user2534365这是一个棘手的问题;我自己想了想,但没有任何想法。我会在几天内,当我有足够的时间集中精力时,再试一次,但我不会给出任何承诺。如果你能幸运地找到answe对于这个问题,请告诉我。找到了一个解决方案来计算停止的次数。而且,我能说你的解决方案是O(n^2)吗?