当前位置:网站首页>LeetCode刷题系列 -- 787. K 站中转内最便宜的航班
LeetCode刷题系列 -- 787. K 站中转内最便宜的航班
2022-08-02 05:01:00 【在河之洲木水】
有 n 个城市通过一些航班连接。给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 pricei 抵达 toi。
现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到出一条最多经过 k 站中转的路线,使得从 src 到 dst 的 价格最便宜 ,并返回该价格。 如果不存在这样的路线,则输出 -1。
示例 1:
输入:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
输出: 200
解释:
城市航班图如下
从城市 0 到城市 2 在 1 站中转以内的最便宜价格是 200,如图中红色所示。
示例 2:
输入:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
输出: 500
解释:
城市航班图如下
从城市 0 到城市 2 在 0 站中转以内的最便宜价格是 500,如图中蓝色所示。
提示:
1 <= n <= 100
0 <= flights.length <= (n * (n - 1) / 2)
flights[i].length == 3
0 <= fromi, toi < n
fromi != toi
1 <= pricei <= 104
航班没有重复,且不存在自环
0 <= src, dst, k < n
src != dst
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/cheapest-flights-within-k-stops
思路:参考大神写的:旅游省钱大法:加权最短路径 :: labuladong的算法小抄
想求到 dst 城市花费最少,那就求与其临近的城市
java代码:
class Solution {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
// 定义 map, key 为 图某个节点,value 为 (入度节点,权重值)
// to -> (from, price)
Map<Integer, List<int[]>> 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<int[]> inValues = new LinkedList<>();
inValues.add(subValue);
map.put(to, inValues);
} else {
map.get(to).add(subValue);
}
}
// 记录表,记录从 src 到 memp[i][j] 的最少花费且中转数为 j
int[][] 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<Integer, List<int[]>> map, int[][] memp) {
if (dst == src) {
return 0;
}
if (k == 0) {
return -1;
}
int res = Integer.MAX_VALUE;
// 当dst 有入度节点时
if (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;
}
}边栏推荐
- 【热题】LeetCode 热题 HOT 100分类+题解
- [QNX Hypervisor 2.2用户手册]9.20 vdev
- navicat新建数据库
- UE4 3DUI显示与交互案例
- 【语义分割】FCN
- navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
- ES6——class类实现继承
- UE4 事件图表不小心拉了很远,找不到一开始创建的节点
- 行测不会概念
- MySQL String Concatenation - Various String Concatenation Practical Cases
猜你喜欢

UE4 利用Mixamo自动绑骨并导入虚幻4

使用pycharm debug 深度学习代码

12 reasons for MySQL slow query
![[Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation](/img/2b/15b3d831bba6aa772ad83f3ac91d23.png)
[Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation

大屏UI设计-看这一篇就够了

go项目的打包部署

数学建模笔记:TOPSIS方法(优劣解距离法)和熵权法修正

matlab simulink 模糊pid结合smith控制温度

迅为RK3568开发板编译Buildroot-全自动编译

The practice of alibaba, data synchronization component canal
随机推荐
从DES走到AES(现代密码的传奇之路)
ES6——class类实现继承
洗牌(DAY 100)
Centos7.9+mysql8.0开启指定IP远程连接数据库
go项目的打包部署
matlab simulink 飞机飞行状态控制
CNN 理解神经网络中卷积(大小,通道数,深度)
MySQL 8.0.29 设置和修改默认密码
UE4 事件图表不小心拉了很远,找不到一开始创建的节点
RADIUS 如何提高 WiFi 无线网络安全性?
公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
MobaXsterm如何使用
MySQL 8.0.29 set and modify the default password
SQL数据表增加列
去字节跳动自动化测试二面原题(根据录音整理)真实有效 26
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
Redis常见题型
Go语学习笔记 - grpc serverclient protobuf 从零开始Go语言
面试测试工程师一般会问什么?测试主管告诉你
HSCTF2022-re题解