Problem: Second Minimum Time to Reach Destination

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`.

Notes:

* 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.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from typing import List, Dict, Tuple, Iterator

class Solution:
  def secondMinimum(self, n: int, edges: List[List[int]], time: int, change: int) -> int:
    graph = [[] for _ in range(n + 1)]
    q = collections.deque([(1, 0)])
    minTime = [[math.inf] * 2 for _ in range(n + 1)]
    minTime[1][0] = 0

    for u, v in edges:
      graph[u].append(v)
      graph[v].append(u)

    while q:
      i, prevTime = q.popleft()

      numChangeSignal = prevTime // change
      waitTime = change - (prevTime % change) if numChangeSignal & 1 else 0
      newTime = prevTime + waitTime + time
      for j in graph[i]:
        if newTime < minTime[j][0]:
          minTime[j][0] = newTime
          q.append((j, newTime))
        elif minTime[j][0] < newTime < minTime[j][1]:
          if j == n:
            return newTime
          minTime[j][1] = newTime
          q.append((j, newTime))


Java solution:
class Solution {
  public int secondMinimum(int n, int[][] edges, int time, int change) {
    List<Integer>[] graph = new List[n + 1];
    // (index, time)
    Queue<Pair<Integer, Integer>> q = new ArrayDeque<>(List.of(new Pair<>(1, 0)));
    // minTime[u][0] := the first minimum time to reach the node u
    // minTime[u][1] := the second minimum time to reach the node u
    int[][] minTime = new int[n + 1][2];
    Arrays.stream(minTime).forEach(A -> Arrays.fill(A, Integer.MAX_VALUE));
    minTime[1][0] = 0;

    for (int i = 1; i <= n; ++i)
      graph[i] = new ArrayList<>();

    for (int[] edge : edges) {
      final int u = edge[0];
      final int v = edge[1];
      graph[u].add(v);
      graph[v].add(u);
    }

    while (!q.isEmpty()) {
      final int u = q.peek().getKey();
      final int prevTime = q.poll().getValue();
      // Start from green.
      // If `numChangeSignal` is odd, now red.
      // If `numChangeSignal` is even, now green.
      final int numChangeSignal = prevTime / change;
      final int waitTime = numChangeSignal % 2 == 0 ? 0 : change - prevTime % change;
      final int newTime = prevTime + waitTime + time;
      for (final int v : graph[u])
        if (newTime < minTime[v][0]) {
          minTime[v][0] = newTime;
          q.offer(new Pair<>(v, newTime));
        } else if (minTime[v][0] < newTime && newTime < minTime[v][1]) {
          if (v == n)
            return newTime;
          minTime[v][1] = newTime;
          q.offer(new Pair<>(v, newTime));
        }
    }

    throw new IllegalArgumentException();
  }
}


C++ solution:
class Solution {
 public:
  int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {
    vector<vector<int>> graph(n + 1);
    queue<pair<int, int>> q{{{1, 0}}};
    // minTime[u][0] := the first minimum time to reach the node u
    // minTime[u][1] := the second minimum time to reach the node u
    vector<vector<int>> minTime(n + 1, vector<int>(2, INT_MAX));
    minTime[1][0] = 0;

    for (const vector<int>& edge : edges) {
      const int u = edge[0];
      const int v = edge[1];
      graph[u].push_back(v);
      graph[v].push_back(u);
    }

    while (!q.empty()) {
      const auto [u, prevTime] = q.front();
      q.pop();
      // Start from green.
      // If `numChangeSignal` is odd, now red.
      // If `numChangeSignal` is even, now green.
      const int numChangeSignal = prevTime / change;
      const int waitTime =
          numChangeSignal % 2 == 0 ? 0 : change - prevTime % change;
      const int newTime = prevTime + waitTime + time;
      for (const int v : graph[u])
        if (newTime < minTime[v][0]) {
          minTime[v][0] = newTime;
          q.emplace(v, newTime);
        } else if (minTime[v][0] < newTime && newTime < minTime[v][1]) {
          if (v == n)
            return newTime;
          minTime[v][1] = newTime;
          q.emplace(v, newTime);
        }
    }

    throw;
  }
};