Posts

Showing posts with the label dijkstra

787. Cheapest Flights Within K Stops

Image
https://leetcode.com/problems/cheapest-flights-within-k-stops/ There are  n  cities connected by  m  flights. Each flight 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 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 = 0 Output: 500 Explanation: 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...

743. Network Delay Time

Image
https://leetcode.com/problems/network-delay-time/ There are  N  network nodes, labelled  1  to  N . Given  times , a list of travel times as  directed  edges  times[i] = (u, v, w) , where  u  is the source node,  v  is the target node, and  w  is the time it takes for a signal to travel from source to target. Now, we send a signal from a certain node  K . How long will it take for all nodes to receive the signal? If it is impossible, return  -1 . Example 1: Input: times = [[2,1,1],[2,3,1],[3,4,1]] , N = 4 , K = 2 Output: 2 Note: N  will be in the range  [1, 100] . K  will be in the range  [1, N] . The length of  times  will be in the range  [1, 6000] . All edges  times[i] = (u, v, w)  will have  1 <= u, v <= N  and  0 <= w <= 100 . --- Related problems 787-cheapest-flights-within-k-stops 1514-path-with-maximum-probability ...