LeetCode 1928.规定时间内到达终点的最小花费:动态规划

【LetMeFly】1928.规定时间内到达终点的最小花费:动态规划

力扣题目链接:https://leetcode.cn/problems/minimum-cost-to-reach-destination-in-time/

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

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

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

给你 maxTimeedgespassingFees ,请你返回完成旅行的 最小费用 ,如果无法在 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 <= x~i~, y~i~ <= n - 1
  • 1 <= time~i~ <= 1000
  • 1 <= passingFees[j] <= 1000
  • 图中两个节点之间可能有多条路径。
  • 图中不含有自环。

解题方法:动态规划

使用dp[t][i]表示在时间t到达城市i所需最小花费。初始值除了dp[0][0] = passingFees[0]外其余dp[t][i]全为"无穷大"10^7

对于当前时间t,枚举每一条边(假设这条边连接ij而花费是cost),若cost <= t,则能更新dp[t][j] = min(dp[t][j], dp[t - cost][i] + passingFees)dp[t][i]同理。

最终,对于所有的tdp[t][n - 1]最小的那个即为最小耗时(若为"无穷大"则返回-1)。

  • 时间复杂度 O ( ( n + m ) × m a x T i m e ) O((n+m)\times maxTime) O((n+m)×maxTime)。其中 n n n是节点数, m m m是边数;构建dp数组耗时 O ( n × m a x T i m e ) O(n\times maxTime) O(n×maxTime),动态规划耗时 O ( m × m a x T i m e ) O(m\times maxTime) O(m×maxTime)
  • 空间复杂度 O ( n × m a x T i m e ) O(n\times maxTime) O(n×maxTime)

AC代码

C++
cpp 复制代码
class Solution {
public:
    int minCost(int maxTime, vector<vector<int>>& edges, vector<int>& passingFees) {
        // vector<vector<pair<int, int>>> graph(passingFees.size());
        // for (vector<int>& edge : edges) {
        //     graph[edge[0]].push_back({edge[1], edge[2]});
        //     graph[edge[1]].push_back({edge[0], edge[2]});
        // }
        vector<vector<int>> dp(maxTime + 1, vector<int>(passingFees.size(), 10000000));
        dp[0][0] = passingFees[0];
        for (int t = 1; t <= maxTime; t++) {
            for (vector<int>& edge : edges) {
                int i = edge[0], j = edge[1], thisTime = edge[2];
                if (thisTime <= t) {
                    dp[t][j] = min(dp[t][j], dp[t - thisTime][i] + passingFees[j]);
                    dp[t][i] = min(dp[t][i], dp[t - thisTime][j] + passingFees[i]);
                }
            }
        }
        int ans = 10000000;
        for (int t = 0; t <= maxTime; t++) {
            ans = min(ans, dp[t].back());
        }
        return ans == 10000000 ? -1 : ans;
    }
};
Go
go 复制代码
package main

func min(a int, b int) int {
    if a <= b {
        return a
    }
    return b
}

func minCost(maxTime int, edges [][]int, passingFees []int) int {
    dp := make([][]int, maxTime + 1)
    // for _, d := range dp {
    //     d = make([]int, len(passingFees))
    //     for i := range d {
    //         d[i] = 10000000
    //     }
    // }
    for th := range dp {
        dp[th] = make([]int, len(passingFees))
        for i := range dp[th] {
            dp[th][i] = 10000000
        }
    }
    dp[0][0] = passingFees[0]
    
    for t := 1; t <= maxTime; t++ {
        for _, edge := range edges {
            i, j, cost := edge[0], edge[1], edge[2]
            if cost <= t {
                dp[t][j] = min(dp[t][j], dp[t - cost][i] + passingFees[j])
                dp[t][i] = min(dp[t][i], dp[t - cost][j] + passingFees[i])
            }
        }
    }

    ans := 10000000
    for _, d := range dp {
        ans = min(ans, d[len(passingFees) - 1])
    }
    if ans == 10000000 {
        return -1
    }
    return ans
}
Java
java 复制代码
import java.util.Arrays;
class Solution {
    public int minCost(int maxTime, int[][] edges, int[] passingFees) {
        int[][] dp = new int[maxTime + 1][passingFees.length];
        for (int i = 0; i <= maxTime; i++) {
            Arrays.fill(dp[i], 10000000);
        }
        dp[0][0] = passingFees[0];
        for (int t = 1; t <= maxTime; t++) {
            for (int[] edge : edges) {
                int i = edge[0], j = edge[1], cost = edge[2];
                if (cost <= t) {
                    dp[t][j] = Math.min(dp[t][j], dp[t - cost][i] + passingFees[j]);
                    dp[t][i] = Math.min(dp[t][i], dp[t - cost][j] + passingFees[i]);
                }
            }
        }
        int ans = 10000000;
        for (int[] d : dp) {
            ans = Math.min(ans, d[passingFees.length - 1]);
        }
        return ans == 10000000 ? -1 : ans;
    }
}
Python
python 复制代码
from typing import List

class Solution:
    def minCost(self, maxTime: int, edges: List[List[int]], passingFees: List[int]) -> int:
        dp = [[10000000] * len(passingFees) for _ in range(maxTime + 1)]
        dp[0][0] = passingFees[0]
        for t in range(1, maxTime + 1):
            for i, j, thisTime in edges:
                if thisTime <= t:
                    dp[t][j] = min(dp[t][j], dp[t - thisTime][i] + passingFees[j])
                    dp[t][i] = min(dp[t][i], dp[t - thisTime][j] + passingFees[i])
        ans = min(d[-1] for d in dp)
        return -1 if ans == 10000000 else ans

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

Tisfy:https://letmefly.blog.csdn.net/article/details/142691241

相关推荐
Themberfue7 分钟前
基础算法之双指针--Java实现(下)--LeetCode题解:有效三角形的个数-查找总价格为目标值的两个商品-三数之和-四数之和
java·开发语言·学习·算法·leetcode·双指针
陈序缘32 分钟前
LeetCode讲解篇之322. 零钱兑换
算法·leetcode·职场和发展
-$_$-34 分钟前
【LeetCode HOT 100】详细题解之二叉树篇
数据结构·算法·leetcode
大白飞飞36 分钟前
力扣203.移除链表元素
算法·leetcode·链表
学无止境\n1 小时前
[C语言]指针和数组
c语言·数据结构·算法
黄俊懿1 小时前
【深入理解SpringCloud微服务】手写实现各种限流算法——固定时间窗、滑动时间窗、令牌桶算法、漏桶算法
java·后端·算法·spring cloud·微服务·架构
新缸中之脑1 小时前
Llama 3.2 安卓手机安装教程
前端·人工智能·算法
夜雨翦春韭1 小时前
【代码随想录Day29】贪心算法Part03
java·数据结构·算法·leetcode·贪心算法
Curry_Math2 小时前
Acwing 区间DP & 计数类DP
算法