Second Minimum Time to Reach Destination - Problem

A city is represented as a bi-directional connected graph with n vertices where each vertex is labeled from 1 to n (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.

The time taken to traverse any edge is time minutes. Each vertex has a traffic signal which changes its color from green to red and vice versa every change minutes. All signals change at the same time. You can enter a vertex at any time, but can leave a vertex only when the signal is green. You cannot wait at a vertex if the signal is green.

The second minimum value is defined as the smallest value strictly larger than the minimum value. For example, the second minimum value of [2, 3, 4] is 3, and the second minimum value of [2, 2, 4] is 4.

Given n, edges, time, and change, return the second minimum time it will take to go from vertex 1 to vertex n.

Note: You can go through any vertex any number of times, including 1 and n. You can assume that when the journey starts, all signals have just turned green.

Input & Output

Example 1 — Basic Graph
$ Input: n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5
Output: 13
💡 Note: First minimum path: 1→4→5 takes 6 time units. Second minimum: 1→3→4→5 or 1→2→1→4→5 takes 13 time units considering traffic light delays.
Example 2 — Simple Path
$ Input: n = 2, edges = [[1,2]], time = 3, change = 2
Output: 11
💡 Note: Only one direct path 1→2. First minimum is 3. To get second minimum, go 1→2→1→2, which takes 11 time units with traffic delays.
Example 3 — Multiple Routes
$ Input: n = 4, edges = [[1,2],[1,3],[2,4],[3,4]], time = 2, change = 4
Output: 6
💡 Note: Two paths: 1→2→4 and 1→3→4. Both take 4 time units (first minimum). Second minimum requires one detour, taking 6 time units.

Constraints

  • 2 ≤ n ≤ 104
  • n - 1 ≤ edges.length ≤ min(2 × 104, n × (n - 1) / 2)
  • edges[i].length == 2
  • 1 ≤ ui, vi ≤ n
  • ui != vi
  • There are no duplicate edges
  • The graph is connected
  • 1 ≤ time, change ≤ 103

Visualization

Tap to expand
Second Minimum Time to Reach Destination INPUT 1 2 3 4 5 n = 5 edges = [[1,2],[1,3], [1,4],[3,4],[4,5]] time = 3 (per edge) change = 5 (signal) ALGORITHM (BFS) 1 Build adjacency list Create graph from edges 2 BFS from node 1 Track 2 shortest times/node 3 Handle traffic signals Wait if red (even cycle) 4 Find 2nd minimum Return dist2[n] BFS Trace to Node 5 Path 1: 1-->4-->5 t=0+3=3, 3+3=6 [min] Path 2: 1-->4-->3-->4-->5 t=3, wait@6 (red) t=10+3=13 [2nd min] GREEN RED FINAL RESULT Second Minimum Path 1 4 3 4 5 +3 +3 +4 +3 Timeline t=0 Start at 1 t=3 At 4, t=6 At 3 t=6-10 WAIT (red) Output: 13 minutes Key Insight: The second minimum path must differ from the minimum by at least one edge traversal. BFS tracks two shortest times per node. Traffic signals add waiting time when arriving during red phase (time/change is odd = green, even = red). Must wait until next green cycle to leave. TutorialsPoint - Second Minimum Time to Reach Destination | BFS Approach
Asked in
Google 12 Facebook 8 Amazon 6
23.5K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen