Stargate Travel

You are given n planets (numbered 1 to n) and a 2D array stargates where stargates[i] = [u, v, w] represents a bidirectional stargate between planets u and v that takes w seconds to travel. You start at planet 1 at second 0 and want to reach planet n.

You are also given a 2D array time of length n. time[i] is a strictly increasing list of seconds when other travelers arrive at planet i + 1. If you try to depart a planet at second t, but t is in its time array, you must wait 1 second. You can only depart at the earliest t' >= t that is not present in the planet's blocked times.

Return the minimum time in seconds to reach planet n. Return -1 if it is impossible.

Example 1:

Input: n = 4, stargates = [[1, 2, 1], [2, 3, 1], [3, 4, 1]], time = [[0, 1], [ ], [ ], [ ] ]

Output: 5

Explanation:

You start at planet 1 at time 0. However, times 0 and 1 are blocked at planet 1. You must wait until time 2 to depart. You take the path 1 -> 2 -> 3 -> 4, arriving at time 2 + 1 + 1 + 1 = 5.

Example 2:

Input: n = 2, stargates = [[1, 2, 10]], time = [[ ], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ]

Output: 10

Explanation:

You travel directly from planet 1 to 2. You arrive at time 10. The blocked times at planet 2 do not affect you since it is your final destination.

Now Your Turn!

Pick the correct output for the given input

Input: n = 3, stargates = [[1, 2, 3]], time = [ [ ], [1, 3], [ ] ]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 <= n <= 105 and 0 <= stargates.length <= 105
  • 1 <= u, v <= n (u != v) and 1 <= w <= 104
  • At most one stargate connection exists between any pair of planets.
  • time.length = n and 0 <= time[i][j] < 109
  • The total number of elements across all time[i] does not exceed 105.
  • Each time[i] is strictly increasing.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int minimumTime(int n, vector<vector<int>>& stargates, vector<vector<int>>& time) {
// Write your code here
}
};
Test Case

Input:

N
Stargates
Time