当前位置:网站首页>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;}} 边栏推荐
- mysql 查询表 所有字段
- 牛客-TOP101-BM41
- Mysql子查询关键字的使用(exists)
- interrupt()、interrupted()和isInterrupted()你真的懂了吗
- 从DES走到AES(现代密码的传奇之路)
- C language: Check for omissions and fill in vacancies (3)
- el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
- Google 安装印象笔记剪藏插件
- JDBC revisited
- Navicat cannot connect to mysql super detailed processing method
猜你喜欢

AMQP协议详解

MySQL导入sql文件的三种方法

MySQL 8.0.28 version installation and configuration method graphic tutorial

一线大厂软件测试流程(思维导图)详解

MySQL 灵魂 16 问,你能撑到第几问?

Detailed explanation of mysql stored procedure

MySql将一张表的数据copy到另一张表中

MySql字符串拆分实现split功能(字段分割转列、转行)

The original question on the two sides of the automatic test of the byte beating (arranged according to the recording) is real and effective 26

matlab simulink 飞机飞行状态控制
随机推荐
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
Redis常见题型
ELK日志分析系统
navicat新建数据库
mysql 8.0.28版本安装配置方法图文教程
swinIR论文阅读笔记
H5接入支付流程-微信支付&支付宝支付
MYSQL 唯一约束
认识消防报警联网中CAN光纤转换器的光纤接口和配套光纤线缆
12 reasons for MySQL slow query
pg数据库报错问题,有懂的吗
Google 安装印象笔记剪藏插件
Does Conway's Law Matter for System Architecture?
golang泛型
MobaXsterm如何使用
Go language study notes - grpc serverclient protobuf Go language from scratch
[Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
Detailed explanation of AMQP protocol
MySQL 5.7 upgrade to 8.0 detailed process
165.比较版本号