【发布时间】:2018-11-18 04:46:35
【问题描述】:
你和你的朋友正开车去提华纳过春假。您正在为旅行省钱,因此您希望将途中的汽油成本降至最低。为了尽量减少您和您的朋友的汽油成本,您和您的朋友整理了以下信息。首先,您的汽车可以在油箱上可靠地行驶 m 英里(但不能再进一步)。您的一个朋友从网上挖掘了加油站数据,并绘制了您路线上的每个加油站以及该加油站的汽油价格。具体来说,他们创建了从最近到最远的 n+1 个加油站价格列表以及两个相邻加油站之间的 n 个距离列表。 Tacoma 是 0 号加油站,Tijuana 是 n 号加油站。为方便起见,他们已将汽油成本转换为您汽车行驶的每英里价格。此外,还计算了两个相邻加油站之间的英里距离。您将以一箱油开始您的旅程,当您到达蒂华纳时,您将为回程加油。您需要确定在哪些加油站停留,以尽量减少旅途中的汽油费用。
示例输入:
价格(每英里美分) [12,14,21,14,17,22,11,16,17,12,30,25,27,24,22,15,24,23,15,21]
距离(英里) [31,42,31,33,12,34,55,25,34,64,24,13,52,33,23,64,43,25,15]
您的汽车一罐油可以行驶 170 英里。
我的输出:
旅行的最低费用为:117.35 美元
加油站停靠:[1, 6, 9, 13, 17, 19]
我已经解决了这个问题,但我不确定我是否以正确的方式解决了问题。如果错了,有人能给我一些建议或指出正确的方向吗?提前谢谢你。
public class GasStation {
/** An array of gas prices.*/
private int[] myPrice;
/** An array of distance between two adjacent gas station.*/
private int[] myDistance;
/** Car's tank capacity.*/
private int myCapacity;
/** List of gas stations to stop at to minimize the cost.*/
private List<Integer> myGasStations;
/**
* A constructor that takes in a price list, distance list, and the car's tank capacity.
* @param thePrice - price list
* @param theDistance - distance list
* @param theCapacity - the car's capacity
*/
public GasStation(final int[] thePrice, final int[] theDistance,
final int theCapacity) {
myPrice = thePrice;
myDistance = theDistance;
myCapacity = theCapacity;
myGasStations = new ArrayList<>();
}
/**
* Calculate for the minimum cost for your trip.
* @return minimum cost
*/
public int calculateMinCost() {
int lenP = myPrice.length;
int lenD = myDistance.length;
if (lenP == 0 || lenD == 0 || myCapacity == 0) return 0;
// gas station -> another gas station (moves)
Map<Integer, Integer> gasD = new HashMap<>();
int[] D = new int[lenD + 1];
D[0] = 0;
// calculate the total distance
for (int i = 0; i < lenD; i++) {
D[i + 1] = D[i] + myDistance[i];
}
int len = D.length;
for (int i = 1; i < len - 1; i++) {
int j = len - 1;
while (D[j] - D[i] >= myCapacity) {
j--;
}
gasD.put(i, j - i);
}
int min = Integer.MAX_VALUE;
for (int i = 1; i < len; i++) {
int temp = 0;
int cur = i;
List<Integer> tempList = new ArrayList<>();
if (D[i] <= myCapacity) {
temp = D[cur] * myPrice[cur];
tempList.add(cur);
int next = gasD.get(cur) + cur;
while (next < len) {
temp += (D[next] - D[cur]) * myPrice[next];
cur = next;
tempList.add(cur);
if (gasD.containsKey(cur)) next = gasD.get(cur) + cur;
else break;
}
if (temp < min) {
min = temp;
myGasStations = tempList;
}
}
}
return min;
}
/**
* Get gas stations to stop at.
* @return a list of gas stations to stop at
*/
public List<Integer> getGasStations() {
return myGasStations;
}
}
【问题讨论】:
-
它看起来太长了,在我看来它要简单得多。那么从塔科马到蒂华纳的单程旅行,你得到了
11735 cents的总费用吗?我得到了7489 cents并在加油站停下来[1, 6, 9, 11, 15, 19] -
补充
31 milesstation 114 cents/mi,补充152 milesstation 611 cents/mi,补充114 milesstation 9@9876754333@9876754333 @@station 1125 cents/mi,121 milesstation 1515 cents/mi,最后到达station 19 -
总费用为
(31 * 14) + (152 * 11) + (114 * 12) + (88 * 25) + (121 * 15) = 7489 cents。我假设我们不需要在station 0(起点)计算满罐(即170 * 12)的成本。好吧,就算我算上,成本还是比你少。如果你好奇,我可以用我使用的方法在下面回复 -
如果我们在
station 19补充回程,费用为147 mi * 21 cents/mi即3087,总费用为7489 + 3087 = 10576,再次低于您的结果 -
你是对的!从那时起,我对我的代码进行了更改,我得到了更好的成本,但它仍然不是像你得到的结果那样的最佳解决方案。您是否建议我的代码中的某个部分修复并改进算法?谢谢!