Fastest Route with Priority Lanes
Upgrad Ride-Share is developing an advanced routing system to provide the fastest possible travel times for its premium users. These users have access to a limited number of "priority lane" passes, which can significantly reduce travel time on congested road segments. Your task is to design an algorithm that calculates the absolute minimum travel time between two points in a city, strategically utilizing these priority passes. This problem is crucial for ensuring customer satisfaction and optimizing operational efficiency for our premium service tier.
You are required to implement a function findFastestRouteWithPriorityLanes that determines the minimum time to travel from a start intersection to an end intersection.
Your solution must satisfy the following:
k priority lane passes. Each pass can be used on one road segment to reduce its travel time to 0 for that specific traversal. A pass can only be used once.Your function will receive the following parameters:
n: An integer representing the total number of intersections in the city, indexed from 0 to n-1.roads: A 2D array of integers, where each inner array [u, v, time] represents an undirected road connecting intersection u and intersection v with a travel time of time.start: An integer representing the starting intersection.end: An integer representing the destination intersection.k: An integer representing the number of priority lane passes available.The function should return:
start to end. If the end intersection is unreachable, return -1.[u, v, time] exists, you can travel from u to v or v to u in time.start and end are the same, the travel time is 0.Examples
Example 1
Input:
n = 3, roads = [[0,1,10],[1,2,5],[0,2,100]], start = 0, end = 2, k = 1
Output:
0
Note:
The fastest route is to use a priority pass on the direct road from 0 to 2, reducing its travel time from 100 to 0. Total time: 0.
Example 2
Input:
n = 4, roads = [[0,1,10],[0,2,2],[1,3,1],[2,3,100]], start = 0, end = 3, k = 1
Output:
1
Note:
The fastest route is to use one priority pass on the road from 0 to 1 (cost 10 -> 0), then travel from 1 to 3 (cost 1). Total time: 0 + 1 = 1.
Example 3
Input:
n = 4, roads = [[0,1,10],[1,2,5],[2,3,1]], start = 0, end = 3, k = 0
Output:
16
Note:
With no priority passes, the route is a standard shortest path: 0 -> 1 (10) -> 2 (5) -> 3 (1). Total time: 16.
Example 4
Input:
n = 3, roads = [[0,1,10]], start = 0, end = 2, k = 0
Output:
-1
Note:
Intersection 2 is unreachable from intersection 0, even with priority passes (as there are no roads connecting to 2 from 0 or 1).
Constraints
1 <= n <= 10^40 <= roads.length <= 10^5roads[i].length == 30 <= u, v < n, u != v1 <= time <= 10000 <= start, end < n0 <= k <= 10 (Number of priority passes is limited to prevent state explosion)k priority lane passes.start == end, or k=0.Loading editor...