Railway Network Delay Optimizer
You are given a network of n nodes labeled from 1 to n. There are `times` directed edges where times[i] = [u, v, w] means a signal travels from node u to node v in w time units.
The signal starts at a given node k. It propagates outward through the network along the directed edges. Your task is to compute the minimum time it takes for the signal to reach all n nodes. Return the time at which the last node receives the signal.
If the signal cannot reach every node (some node is unreachable from k), return -1.
For example, with times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, and k = 2, the signal reaches node 1 in 1, node 3 in 1, and node 4 in 2 units. The last node (4) receives it at time 2, so the answer is 2. If a node is unreachable, like times = [[1,2,1]] with n=2 and k=2, node 1 cannot be reached, so the answer is -1.
This is the classic single-source shortest path problem on a weighted directed graph, best solved with Dijkstra's algorithm using a priority queue. Because edge weights are non-negative, Dijkstra guarantees the first time a node is finalized its distance is minimal.
Edge cases include an empty graph (if k is the only node, the answer is 0), a start node that is also the only reachable node (return 0), and graphs where some nodes form an unreachable component (return -1).
Example Input & Output
Signal reaches node 4 last at time 2.
Node 1 is unreachable from node 2.
Best path 1 -> 2 -> 3 arrives at time 3.
Algorithm Flow
Solution Approach
This is the classic single-source shortest path problem on a weighted directed graph, best solved with Dijkstra's algorithm. The signal starts at node k and propagates along directed edges, and we want the time when the last node receives it — in other words, the maximum shortest distance from k to any other node.
Because all edge weights are non-negative, Dijkstra's algorithm is correct and efficient. It visits nodes in order of increasing distance, and the first time a node is finalized its distance is guaranteed to be minimal. If any node is unreachable, we return -1.
Here is the implementation:
We build a directed adjacency list from the times edges, then initialize the distance to the start as 0 and everything else as infinity. A priority queue (simulated with a sorted array here) always extracts the closest unprocessed node. When a shorter path to a neighbor is found, we relax it and push it onto the queue.
After the search, we scan all nodes. If any distance is still infinity, the node is unreachable and we return -1. Otherwise the answer is the maximum distance, which is the time the last node receives the signal.
Let us verify with times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2. Node 1 is reached in 1, node 3 in 1, and node 4 via 3 in 2. The maximum is 2, which is the answer. For times = [[1,2,1]], n = 2, k = 2, node 1 is unreachable, so we return -1.
The time complexity is O((n + e) log n) with a real priority queue, and the space complexity is O(n + e).
Best Answers
import java.util.*;
class Solution {
public int optimize_railway_delay(int[][] times, int n, int k) {
List<int[]>[] adj = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) adj[i] = new ArrayList<>();
for (int[] t : times) adj[t[0]].add(new int[]{t[1], t[2]});
int[] dist = new int[n + 1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[k] = 0;
boolean[] visited = new boolean[n + 1];
for (int it = 0; it < n; it++) {
int u = -1, best = Integer.MAX_VALUE;
for (int v = 1; v <= n; v++) {
if (!visited[v] && dist[v] < best) { best = dist[v]; u = v; }
}
if (u == -1) break;
visited[u] = true;
for (int[] e : adj[u]) {
int v = e[0], w = e[1];
if (dist[u] + w < dist[v]) dist[v] = dist[u] + w;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (dist[i] == Integer.MAX_VALUE) return -1;
ans = Math.max(ans, dist[i]);
}
return ans;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
