当前位置:网站首页>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;}} 边栏推荐
- 在 .NET MAUI 中如何更好地自定义控件
- Redis常见题型
- 腾讯注册中心演进及性能优化实践
- JDBC revisited
- Browser onload event
- el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
- MySQL implements sorting according to custom (specified order)
- Go语学习笔记 - grpc serverclient protobuf 从零开始Go语言
- Google 安装印象笔记剪藏插件
- How much does a test environment cost? Start with cost and efficiency
猜你喜欢

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

MySql copies data from one table to another table

C language: Check for omissions and fill in vacancies (3)

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

Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)

Introduction and use of apifox (1).

MYSQL 唯一约束

【热题】LeetCode 热题 HOT 100分类+题解

H5接入支付流程-微信支付&支付宝支付

ELK日志分析系统
随机推荐
安全测试常见问题
mysql实现按照自定义(指定顺序)排序
[QNX Hypervisor 2.2用户手册]9.20 vdev
【HCIE】NO.30 OSPFv3的基本配置
Redis常见题型
100 latest software testing interview questions in 2022, summary of common interview questions and answers
MySQL 用户授权
How much does a test environment cost? Start with cost and efficiency
简道云-灵活易用的应用搭建平台
H5如何实现唤起APP
el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
合作的小伙伴,缺乏主人翁(owner)意识,好苦恼
选择黑盒测试用例设计方法的综合策略方案总结
物联网通信协议全解析
ELK log analysis system
Detailed explanation of AMQP protocol
Google Chrome(谷歌浏览器)安装使用
2022年7月学习计划完成情况
认识消防报警联网中CAN光纤转换器的光纤接口和配套光纤线缆
在 .NET MAUI 中如何更好地自定义控件