博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 787. Cheapest Flights Within K Stops
阅读量:4621 次
发布时间:2019-06-09

本文共 3362 字,大约阅读时间需要 11 分钟。

原题链接在这里:

题目:

There are n cities connected by m flights. Each fight starts from city 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 srcto 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 = 1Output: 200Explanation: The graph looks like this:

The cheapest price from city 0 to city 2 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 = 0Output: 500Explanation: The graph looks like this:

The cheapest price from city 0 to city 2 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 from 0 to n - 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: 

1 class Solution { 2     public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) { 3         if(n == 0 || K < 0 || flights == null || flights.length == 0 || flights[0].length != 3){ 4             return -1; 5         } 6          7         int res = Integer.MAX_VALUE; 8          9         Map
> graph = new HashMap<>();10 for(int [] f : flights){11 if(!graph.containsKey(f[0])){12 graph.put(f[0], new ArrayList
());13 }14 15 graph.get(f[0]).add(new int[]{f[1], f[2]});16 }17 18 PriorityQueue
que = new PriorityQueue
((a, b) -> a.cost-b.cost);19 que.add(new Node(src, 0, -1));20 while(!que.isEmpty()){21 Node cur = que.poll();22 23 if(cur.city == dst){24 return cur.cost;25 }26 27 if(cur.stop < K){28 List
nexts = graph.getOrDefault(cur.city, new ArrayList
());29 for(int [] next : nexts){30 que.add(new Node(next[0], cur.cost+next[1], cur.stop+1));31 }32 }33 }34 35 return -1;36 }37 }38 39 class Node{40 int city;41 int cost;42 int stop;43 public Node(int city, int cost, int stop){44 this.city = city;45 this.cost = cost;46 this.stop = stop;47 }48 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11489826.html

你可能感兴趣的文章
Excel导出POI
查看>>
兼容性
查看>>
自动执行sftp命令的脚本
查看>>
转 Merkle Tree(默克尔树)算法解析
查看>>
网络编程基础之socket编程
查看>>
各种浏览器的user-agent和
查看>>
Restful levels
查看>>
Phonegap移动开发:布局总结(一) 全局
查看>>
Java 变参函数的实现
查看>>
nrf51 SDK自带例程的解读
查看>>
SESSION技术
查看>>
数据结构(五)之直接插入排序
查看>>
SQL函数——LENGTH()和LENGTHB()
查看>>
vim - manual -个人笔记
查看>>
详解Javascript中prototype属性(推荐)
查看>>
angularjs实现首页轮播图
查看>>
Git 对象 和checkout 和stash的笔记
查看>>
团队项目总结2-服务器通信模型和顺序图
查看>>
hdu 1085 Holding Bin-Laden Captive!
查看>>
[周记]8.7~8.16
查看>>