Railway Network Delay Optimizer
This one is about reading carefully and then following a clear rule. In Railway Network Delay Optimizer, you are trying to work toward the right number by following one clear idea.
This problem feels a bit like a maze or map challenge. You need to think about how moves, paths, or links work together. Some places may still connect nicely, while others may be blocked or no longer fit the rule. The answer comes from following those connections carefully.
For example, if the input is n = 4, routes = [[0,1,3,2],[1,3,4,5],[0,2,10,0],[2,3,4,1]], start = 0, destination = 3, skip_stations = [1], waiver_budget = 0, the answer is 15. With station 1 offline the driver travels 0 → 2 → 3 and pays the unavoidable penalty on the final segment. Another example is n = 5, routes = [[0,1,5,1],[1,2,4,2],[2,4,3,2],[0,3,6,0]], start = 0, destination = 4, skip_stations = [2], waiver_budget = 1, which gives -1. Every viable path requires visiting station 2, so the destination remains unreachable.
This is one of the harder problems, so it is normal if the answer is not obvious right away. The key is keeping track of where you can move and which routes still follow the rule.
Example Input & Output
With station 1 offline the driver travels 0 → 2 → 3 and pays the unavoidable penalty on the final segment.
Every viable path requires visiting station 2, so the destination remains unreachable.
The path 0 → 1 → 4 → 3 uses two waivers to ignore the largest penalties and beats the direct express lane.
Algorithm Flow

Best Answers
import java.util.*;
class Solution {
public int optimize_railway_delay(int[][] times, int n, int k) {
Map<Integer, List<int[]>> adj = new HashMap<>();
for (int[] t : times) {
adj.computeIfAbsent(t[0], x -> new ArrayList<>()).add(new int[]{t[1], t[2]});
}
int[] dist = new int[n + 1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[k] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
pq.add(new int[]{0, k});
while (!pq.isEmpty()) {
int[] curr = pq.poll();
int d = curr[0], u = curr[1];
if (d > dist[u]) continue;
if (!adj.containsKey(u)) continue;
for (int[] v : adj.get(u)) {
if (dist[u] + v[1] < dist[v[0]]) {
dist[v[0]] = dist[u] + v[1];
pq.add(new int[]{dist[v[0]], v[0]});
}
}
}
int maxDist = 0;
for (int i = 1; i <= n; i++) {
if (dist[i] == Integer.MAX_VALUE) return -1;
maxDist = Math.max(maxDist, dist[i]);
}
return maxDist;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
