Approach
Breadth-first search
For Minimum Time to Visit Disappearing Nodes, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 51 lines of Java from the credited upstream file 3112.java.
- The implementation visibly relies on sequence storage, work queue.
- 5 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int[] minimumTime(int n, int[][] edges, int[] disappear) {3 List<Pair<Integer, Integer>>[] graph = new List[n];4 5 for (int i = 0; i < n; i++)6 graph[i] = new ArrayList<>();7 8 for (int[] edge : edges) {9 final int u = edge[0];10 final int v = edge[1];11 final int w = edge[2];12 graph[u].add(new Pair<>(v, w));13 graph[v].add(new Pair<>(u, w));14 }15 16 return dijkstra(graph, 0, disappear);17 }18 19 private int[] dijkstra(List<Pair<Integer, Integer>>[] graph, int src, int[] disappear) {20 int[] dist = new int[graph.length];21 Arrays.fill(dist, Integer.MAX_VALUE);22 23 dist[src] = 0;24 Queue<Pair<Integer, Integer>> minHeap =25 new PriorityQueue<>(Comparator.comparingInt(Pair::getKey)) {26 { offer(new Pair<>(dist[src], src)); } 27 };28 29 while (!minHeap.isEmpty()) {30 final int d = minHeap.peek().getKey();31 final int u = minHeap.poll().getValue();32 if (d > dist[u])33 continue;34 for (Pair<Integer, Integer> pair : graph[u]) {35 final int v = pair.getKey();36 final int w = pair.getValue();37 if (d + w < disappear[v] && d + w < dist[v]) {38 dist[v] = d + w;39 minHeap.offer(new Pair<>(dist[v], v));40 }41 }42 }43 44 for (int i = 0; i < dist.length; ++i)45 if (dist[i] == Integer.MAX_VALUE)46 dist[i] = -1;47 48 return dist;49 }50}51