当前位置:网站首页>LeetCode brush topic series - 787 K station transfer within the cheapest flight
LeetCode brush topic series - 787 K station transfer within the cheapest flight
2022-08-02 06:12:00 【Wooden water in the river island】
There are n cities connected by some flights.You are given an array flights , where flights[i] = [fromi, toi, pricei] , which means that the flights all start from city fromi and arrive at toi at price pricei .
Now given all the cities and flights, as well as the departure city src and the destination dst, your task is to find a route with at most k stops to make the cheapest price from src to dst, and return that price.If no such route exists, output -1.
Example 1:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Explanation:
The city flight map is as follows
The cheapest price from city 0 to city 2 within 1 stop is 200, as shown in red in the picture.
Example 2:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500
Explanation:
The city flight map is as follows
The cheapest price from city 0 to city 2 within 0 stops is 500, shown in blue.
Tip:
1 <= n <= 100
0 <= flights.length <= (n * (n - 1) / 2)
flights[i].length == 3
0 <=fromi, toi < n
fromi != toi
1 <= pricei <= 104
No duplicate flights and no self-loops
0 <= src, dst, k < n
src != dst
Source: LeetCode
Link: https://leetcode.cn/problems/cheapest-flights-within-k-stops
Ideas: Refer to what the Great God wrote: Tourism Saving Money: Weighted Shortest Path :: labuladong's algorithm cheat sheet
If you want to find the city with the least dst cost, then find the city near it
java code:
class Solution {public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {// Define map, key is a node in the graph, value is (in-degree node, weight value)// to -> (from, price)Map> map = new HashMap<>();for (int[] dists : flights) {int from = dists[0];int to = dists[1];int price = dists[2];int[] subValue = {from, price};if (!map.containsKey(to)) {List inValues = new LinkedList<>();inValues.add(subValue);map.put(to, inValues);} else {map.get(to).add(subValue);}}// record table, record the minimum cost from src to memp[i][j] and the number of transfers is jint[][] memp = new int[n][k + 1];for (int[] value : memp) {Arrays.fill(value, -888);}return dp(src, dst, k+1, map, memp);}int dp(int src, int dst, int k, Map> map, int[][] memp) {if (dst == src) {return 0;}if (k == 0) {return -1;}int res = Integer.MAX_VALUE;// when dst has in-degree nodesif (map.containsKey(dst)) {for (int[] subValues : map.get(dst)) {int from = subValues[0];int price = subValues[1];int fromValue = memp[from][k - 1];if (fromValue == -888) {fromValue = dp(src, from, k - 1, map, memp);memp[from][k - 1] = fromValue;}if (fromValue != -1) {res = Math.min(res, price + fromValue);}}}return res == Integer.MAX_VALUE ? -1 : res;}} 边栏推荐
- 2022河南萌新联赛第(四)场:郑州轻工业大学 C - 最大公因数
- 浏览器的onload事件
- Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
- MySQL 游标
- el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
- navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
- go项目的打包部署
- golang's time package: methods for time interval formatting and output of timestamp formats such as seconds, milliseconds, and nanoseconds
- swinIR论文阅读笔记
- "Digital reconstruction of the system, getting the CEO is the first step"
猜你喜欢

IOT物联网概述及应用层架构入门篇

ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法

Go语言之interface详解

Redis-集群模式(主从复制模式,哨兵模式,集群化模式)

认识CAN光纤转换器的光纤接口和配套光纤线缆

apifox介绍及使用(1)。

Detailed explanation of AMQP protocol

interrupt()、interrupted()和isInterrupted()你真的懂了吗

MySQL 多表关联一对多查询实现取最新一条数据

MySQL String Concatenation - Various String Concatenation Practical Cases
随机推荐
Does Conway's Law Matter for System Architecture?
Go语学习笔记 - grpc serverclient protobuf 从零开始Go语言
行测不会概念
Go language study notes - grpc serverclient protobuf Go language from scratch
Mysql return table
MySQL如何创建用户
[PSQL] 函数、谓词、CASE表达式、集合运算
一线大厂软件测试流程(思维导图)详解
【MLT】MLT多媒体框架生产消费架构解析(一)
MySQL 游标
apifox介绍及使用(1)。
LeetCode刷题系列 -- 10. 正则表达式匹配
MySQL导入sql文件的三种方法
Mysql常用命令大全
Grid布局介绍
MySQL String Concatenation - Various String Concatenation Practical Cases
PDF file conversion format
MYSQL unique constraint
go语言中的goroutine(协程)
认识CAN光纤转换器的光纤接口和配套光纤线缆