LeetCode 题解工作台

规定时间内到达终点的最小花费

一个国家有 n 个城市,城市编号为 0 到 n - 1 ,题目保证 所有城市 都由双向道路 连接在一起 。道路由二维整数数组 edges 表示,其中 edges[i] = [x i , y i , time i ] 表示城市 x i 和 y i 之间有一条双向道路,耗费时间为 time i 分钟。两…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

我们定义 表示经过 分钟,从城市 到达城市 的最小花费。初始时 $f[0][0] = \textit{passingFees}[0]$,其余的 $f[0][j] = +\infty$。 接下来,我们在 $[1, \textit{maxTime}]$ 的时间范围内,遍历所有的边,对于每一条边 $(x, y, t)$,如果 $t \leq i$,那么我们:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

一个国家有 n 个城市,城市编号为 0 到 n - 1 ,题目保证 所有城市 都由双向道路 连接在一起 。道路由二维整数数组 edges 表示,其中 edges[i] = [xi, yi, timei] 表示城市 xi 和 yi 之间有一条双向道路,耗费时间为 timei 分钟。两个城市之间可能会有多条耗费时间不同的道路,但是不会有道路两头连接着同一座城市。

每次经过一个城市时,你需要付通行费。通行费用一个长度为 n 且下标从 0 开始的整数数组 passingFees 表示,其中 passingFees[j] 是你经过城市 j 需要支付的费用。

一开始,你在城市 0 ,你想要在 maxTime 分钟以内 (包含 maxTime 分钟)到达城市 n - 1 。旅行的 费用 为你经过的所有城市 通行费之和 (包括 起点和终点城市的通行费)。

给你 maxTimeedges 和 passingFees ,请你返回完成旅行的 最小费用 ,如果无法在 maxTime 分钟以内完成旅行,请你返回 -1 。

 

示例 1:

输入:maxTime = 30, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
输出:11
解释:最优路径为 0 -> 1 -> 2 -> 5 ,总共需要耗费 30 分钟,需要支付 11 的通行费。

示例 2:

输入:maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
输出:48
解释:最优路径为 0 -> 3 -> 4 -> 5 ,总共需要耗费 26 分钟,需要支付 48 的通行费。
你不能选择路径 0 -> 1 -> 2 -> 5 ,因为这条路径耗费的时间太长。

示例 3:

输入:maxTime = 25, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
输出:-1
解释:无法在 25 分钟以内从城市 0 到达城市 5 。

 

提示:

  • 1 <= maxTime <= 1000
  • n == passingFees.length
  • 2 <= n <= 1000
  • n - 1 <= edges.length <= 1000
  • 0 <= xi, yi <= n - 1
  • 1 <= timei <= 1000
  • 1 <= passingFees[j] <= 1000 
  • 图中两个节点之间可能有多条路径。
  • 图中不含有自环。
lightbulb

解题思路

方法一:动态规划

我们定义 f[i][j]f[i][j] 表示经过 ii 分钟,从城市 00 到达城市 jj 的最小花费。初始时 f[0][0]=passingFees[0]f[0][0] = \textit{passingFees}[0],其余的 f[0][j]=+f[0][j] = +\infty

接下来,我们在 [1,maxTime][1, \textit{maxTime}] 的时间范围内,遍历所有的边,对于每一条边 (x,y,t)(x, y, t),如果 tit \leq i,那么我们:

  • 可以先经过 iti - t 分钟,从城市 00 到达城市 yy,然后再经过 tt 分钟,从城市 yy 到达城市 xx,再加上到达城市 xx 的通行费,即 f[i][x]=min(f[i][x],f[it][y]+passingFees[x])f[i][x] = \min(f[i][x], f[i - t][y] + \textit{passingFees}[x])
  • 也可以先经过 iti - t 分钟,从城市 00 到达城市 xx,然后再经过 tt 分钟,从城市 xx 到达城市 yy,再加上到达城市 yy 的通行费,即 f[i][y]=min(f[i][y],f[it][x]+passingFees[y])f[i][y] = \min(f[i][y], f[i - t][x] + \textit{passingFees}[y])

最终答案即为 min{f[i][n1]}\min\{f[i][n - 1]\},其中 i[0,maxTime]i \in [0, \textit{maxTime}]。如果答案为 ++\infty,则返回 1-1

时间复杂度 O(maxTime×(m+n))O(\textit{maxTime} \times (m + n)),其中 mmnn 分别是边的数量和城市的数量。空间复杂度 O(maxTime×n)O(\textit{maxTime} \times n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
    def minCost(
        self, maxTime: int, edges: List[List[int]], passingFees: List[int]
    ) -> int:
        m, n = maxTime, len(passingFees)
        f = [[inf] * n for _ in range(m + 1)]
        f[0][0] = passingFees[0]
        for i in range(1, m + 1):
            for x, y, t in edges:
                if t <= i:
                    f[i][x] = min(f[i][x], f[i - t][y] + passingFees[x])
                    f[i][y] = min(f[i][y], f[i - t][x] + passingFees[y])
        ans = min(f[i][n - 1] for i in range(m + 1))
        return ans if ans < inf else -1
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for an efficient way to manage state transitions across cities and times.

  • question_mark

    Pay attention to how the candidate handles edge cases, such as when it's impossible to reach the destination in time.

  • question_mark

    Evaluate the candidate's approach to optimizing performance using priority queues or dynamic programming.

warning

常见陷阱

外企场景
  • error

    Ignoring the time constraint and exploring paths that exceed maxTime.

  • error

    Not considering multiple paths between two cities with different travel times.

  • error

    Overlooking edge cases where the destination is unreachable within the time limit.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Introduce multiple source and destination cities.

  • arrow_right_alt

    Modify the problem by allowing one-way roads.

  • arrow_right_alt

    Vary the passing fees by making them dynamic based on time of day or other conditions.

help

常见问题

外企场景

规定时间内到达终点的最小花费题解:状态·转移·动态规划 | LeetCode #1928 困难