LeetCode 787. Cheapest Flights Within K Stops
原题链接在这里:https://leetcode.com/problems/cheapest-flights-within-k-stops/
题目:
There are n
cities connected by m
flights. Each fight starts from city u
and arrives at v
with a price w
.
Now given all the cities and flights, together with starting city src
and the destination dst
, your task is to find the cheapest price from src
to dst
with up to k
stops. If there is no such route, 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 graph looks like this:
The cheapest price from city0
to city2
with at most 1 stop costs 200, as marked 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 graph looks like this:
The cheapest price from city0
to city2
with at most 0 stop costs 500, as marked blue in the picture.
Note:
- The number of nodes
n
will be in range[1, 100]
, with nodes labeled from0
ton
- 1
. - The size of
flights
will be in range[0, n * (n - 1) / 2]
. - The format of each flight will be
(src,
dst
, price)
. - The price of each flight will be in the range
[1, 10000]
. k
is in the range of[0, n - 1]
.- There will not be any duplicated flights or self cycles.
题解:
When it comes to Dijkstra, it needs PriorityQueue based on cost.
From the example, if the stops is within K, then it could continue update the total cost.
Create a map first.
Initialize PriorityQueue based on current cost. Put src in it.
When polling out head node, check if it is dst, if it is, it must be smallest cost because of PriorityQueue.
Otherwise, get its stop, it is still smaller than K, and check the graph, get next nestinations and accumlate the cost, add to PriorityQueue.
If until the PriorityQueue is empty, there is no return yet, then there is no such route, return -1.
Time Complexity: O(ElogE). E = flights.length. que size could be E, each add and poll takes logE.
Space: O(E). graph size is O(E). que size is O(E).
AC Java:
class Solution {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
if(n == 0 || K < 0 || flights == null || flights.length == 0 || flights[0].length != 3){
return -1;
} int res = Integer.MAX_VALUE; Map<Integer, List<int []>> graph = new HashMap<>();
for(int [] f : flights){
if(!graph.containsKey(f[0])){
graph.put(f[0], new ArrayList<int []>());
} graph.get(f[0]).add(new int[]{f[1], f[2]});
} PriorityQueue<Node> que = new PriorityQueue<Node>((a, b) -> a.cost-b.cost);
que.add(new Node(src, 0, -1));
while(!que.isEmpty()){
Node cur = que.poll(); if(cur.city == dst){
return cur.cost;
} if(cur.stop < K){
List<int []> nexts = graph.getOrDefault(cur.city, new ArrayList<int []>());
for(int [] next : nexts){
que.add(new Node(next[0], cur.cost+next[1], cur.stop+1));
}
}
} return -1;
}
} class Node{
int city;
int cost;
int stop;
public Node(int city, int cost, int stop){
this.city = city;
this.cost = cost;
this.stop = stop;
}
}
LeetCode 787. Cheapest Flights Within K Stops的更多相关文章
- [LeetCode] 787. Cheapest Flights Within K Stops K次转机内的最便宜航班
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...
- 【LeetCode】787. Cheapest Flights Within K Stops 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 参考资料 日期 题目 ...
- [LeetCode] 787. Cheapest Flights Within K Stops_Medium tag: Dynamic Programming, BFS, Heap
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...
- 787. Cheapest Flights Within K Stops
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...
- [LeetCode] Cheapest Flights Within K Stops K次转机内的最便宜的航班
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...
- [Swift]LeetCode787. K 站中转内最便宜的航班 | Cheapest Flights Within K Stops
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...
- Within K stops 最短路径 Cheapest Flights Within K Stops
2018-09-19 22:34:28 问题描述: 问题求解: 本题是典型的最短路径的扩展题,可以使用Bellman Ford算法进行求解,需要注意的是在Bellman Ford算法的时候需要额外申请 ...
- Java实现 LeetCode 787 K 站中转内最便宜的航班(两种DP)
787. K 站中转内最便宜的航班 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是 ...
- LeetCode——787. K 站中转内最便宜的航班
有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst 最多经过 ...
随机推荐
- JAVA十六进制数据接收与传输
一.十六进制转换工具类 主要包含十六进制字符串转ASCII,ASCII转十六进制字符串以及十六进制字符串转Byte数组等方法: /** * Created by wly on 2018/4/17. * ...
- 认证授权-学习笔记2-OpenId Connect
简介 简单来说:OIDC是OpenID Connect的简称,OIDC=(Identity, Authentication) + OAuth 2.0.它在OAuth2上构建了一个身份层,是一个基于OA ...
- OpenResty部署nginx及nginx+lua
因为用nginx+lua去开发,所以会选择用最流行的开源方案,就是用OpenResty nginx+lua打包在一起,而且提供了包括redis客户端,mysql客户端,http客户端在内的大量的组件 ...
- JAVA8的java.util.function包
一 概述 name type description Consumer Consumer< T > 接收T对象,不返回值 Predicate Predicate< T > 接收 ...
- C# vb .net实现裁剪效果特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的裁剪效果效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...
- Centos复制的系统无法获取IP地址
本文主要是解决自己玩虚拟机时遇到的问题,网上查询了很多资料,最好综合多方的资料进行如下总结,如果无法解决您得问题,不要着急慢慢梳理总会解决的,加油~~~ 1.添加一块新的虚拟机的网卡2.删除rm -r ...
- js获取列表多条数据(接口)
读取数据://ajax去服务器端校验 $.ajax({ type:"post", url:"http://", data:{deviceid:1}, dataT ...
- margin 外边距合并问题
一.兄弟元素的外边距合并 效果图如下:(二者之间的间距为100px,不是150px) 二.嵌套元素的外边距合并 对于两个嵌套关系的元素,如果父元素中没有内容或者内容在子元素的后面并且没有上内边距及边框 ...
- 结队编程--java实现
1.GitHub地址:https://github.com/caiyouling/Myapp 队友:钟小敏 GitHub地址:https://github.com/zhongxiao136/Myapp ...
- Python学习日记(二十九) 网络编程
早期的计算机通信需要有一个中间件,A要给B传东西,A必须要把信息传给中间件,B再把从中间件中拿到信息 由于不同机器之间需要通信就产生了网络 软件开发的架构 1.C/S架构 服务器-客户机,即Clien ...