leetcode
leetcode 2251 ~ 2300
前往目标的最小代价

前往目标的最小代价

难度:

标签:

题目描述

You are given an array start where start = [startX, startY] represents your initial position (startX, startY) in a 2D space. You are also given the array target where target = [targetX, targetY] represents your target position (targetX, targetY).

The cost of going from a position (x1, y1) to any other position in the space (x2, y2) is |x2 - x1| + |y2 - y1|.

There are also some special roads. You are given a 2D array specialRoads where specialRoads[i] = [x1i, y1i, x2i, y2i, costi] indicates that the ith special road can take you from (x1i, y1i) to (x2i, y2i) with a cost equal to costi. You can use each special road any number of times.

Return the minimum cost required to go from (startX, startY) to (targetX, targetY).

 

Example 1:

Input: start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
Output: 5
Explanation: The optimal path from (1,1) to (4,5) is the following:
- (1,1) -> (1,2). This move has a cost of |1 - 1| + |2 - 1| = 1.
- (1,2) -> (3,3). This move uses the first special edge, the cost is 2.
- (3,3) -> (3,4). This move has a cost of |3 - 3| + |4 - 3| = 1.
- (3,4) -> (4,5). This move uses the second special edge, the cost is 1.
So the total cost is 1 + 2 + 1 + 1 = 5.
It can be shown that we cannot achieve a smaller total cost than 5.

Example 2:

Input: start = [3,2], target = [5,7], specialRoads = [[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]]
Output: 7
Explanation: It is optimal to not use any special edges and go directly from the starting to the ending position with a cost |5 - 3| + |7 - 2| = 7.

 

Constraints:

  • start.length == target.length == 2
  • 1 <= startX <= targetX <= 105
  • 1 <= startY <= targetY <= 105
  • 1 <= specialRoads.length <= 200
  • specialRoads[i].length == 5
  • startX <= x1i, x2i <= targetX
  • startY <= y1i, y2i <= targetY
  • 1 <= costi <= 105

代码结果

运行时间: 74 ms, 内存: 16.2 MB


解释

方法:

该题解采用了类似于Dijkstra算法的策略来解决从初始位置到目标位置的最小代价问题。首先,初始化一个距离字典`dist`,其中初始位置的距离设为0,其余位置距离为无穷大。使用一个集合`vis`来记录已经访问过的点。在算法执行过程中,每次从未访问的点中选择一个当前距离最小的点作为当前处理的点。如果该点为目标点,则返回其对应的距离值。对选出的点进行扩展,更新其通过普通移动或特殊路径到其他点的距离。普通移动的代价为坐标差的绝对值和,特殊路径的代价为在路径起点的代价加上特定成本`costi`。

时间复杂度:

O(n^2 + nm)

空间复杂度:

O(n)

代码细节讲解

🦆
题解中使用的是类似Dijkstra算法,但没有使用优先队列来优化查找最小距离点的过程。在实际编程中,这种方法会有哪些性能影响?
在Dijkstra算法中,使用优先队列可以有效地从未访问的节点中快速找到当前距离最小的节点。如果不使用优先队列,如题解所示,每次寻找最小距离节点需要遍历所有节点,复杂度是O(n)。这种遍历方式在节点数量较多时会显著增加计算时间,导致整体算法效率下降,尤其是在稀疏图中,使用优先队列可以将时间复杂度降低到O((V+E)logV),其中V是节点数,E是边数。
🦆
在处理特殊路径时,题解中是如何确保每条特殊路径只被使用一次,而不会导致重复计算或循环引用呢?
题解中并没有特别说明如何避免特殊路径的重复使用或循环引用。在实际实现中,可以通过在访问集合`vis`中记录已经处理过的节点来避免重复处理。此外,若存在循环引用的可能,需要设计额外的逻辑来检测和阻止这种情况,例如使用一个字典来记录每个节点的访问次数并设定阈值,或者在发现循环时终止算法。
🦆
题解中提到的距离更新公式`dist[w] = min(dist[w], dv + abs(x1 - vx) + abs(y1 - vy) + cost)`,这里的`abs(x1 - vx) + abs(y1 - vy)`是基于什么考虑?是否应该是`dv + cost`?
题解中的`abs(x1 - vx) + abs(y1 - vy)`表示从当前节点v到特殊路径起点(x1, y1)的曼哈顿距离。这个距离加上特殊路径的成本`cost`和当前点到v的最短距离`dv`合在一起,才是从起始点经过v到达特殊路径终点w的总代价。因此,这里的表达式是正确的,考虑到了从当前节点到特殊路径起点的移动成本。
🦆
题解中如何处理从`specialRoads`数组提取的特殊路径信息,以确保它们可以被有效且正确地应用于距离的更新?
题解中通过遍历`specialRoads`数组,对每条特殊路径进行处理。对于数组中的每个元素,提取起点(x1, y1)和终点(x2, y2)的坐标以及路径的特定成本`cost`。然后,基于当前节点v的已知最短距离dv,计算到达特殊路径终点w的可能最短距离,并使用`min`函数更新距离字典`dist`中w点的值。这种方法确保了每次算法只会试图优化到达每个节点的最短距离。

相关问题