BudiBadu Logo

Railway Network Delay Optimizer

Array Hard 2 views
Like24

Railway Network Delay Optimizer is a shortest-path problem with an extra resource constraint Each track has base travel time plus optional delay penalty and you have a limited waiver budget that can ignore penalties on selected segments You must minimize total travel time from start to destination while respecting closure rules for skipped stations and handling duplicate connections safely The right tool is modified Dijkstra on expanded state Instead of distance by node only track distance by node waivers_used For each edge you can move without waiver pay base penalty or if budget remains consume one waiver and pay base only Priority queue ordering still uses current total time Also enforce closed-station constraints during graph construction or traversal so invalid routes are never explored The judge checks reachable and unreachable networks empty-edge cases and scenarios where spending waivers early vs late changes optimality Return 1 if destination cannot be reached under constraints Output is one integer minimum time deterministic and free of debug text This hard problem is great practice for multi-dimensional shortest path modeling where path quality depends on both location and how much special resource has already been consumed A useful guard is to skip stale heap states whenever the popped distance is greater than the recorded best for that exact state That keeps exploration tight and avoids unnecessary relaxations which is especially important when the waiver dimension multiplies the number of traversable states When the task involves

Examples

Example 1
Input
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
Output
15
Explanation

With station 1 offline the driver travels 0 → 2 → 3 and pays the unavoidable penalty on the final segment.

Example 2
Input
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
Output
-1
Explanation

Every viable path requires visiting station 2, so the destination remains unreachable.

Example 3
Input
n = 6, routes = [[0,1,4,6],[1,2,6,5],[2,3,5,2],[0,3,18,0],[1,4,3,4],[4,3,4,3]], start = 0, destination = 3, skip_stations = [], waiver_budget = 2
Output
14
Explanation

The path 0 → 1 → 4 → 3 uses two waivers to ignore the largest penalties and beats the direct express lane.

Algorithm Flow

Recommendation Algorithm Flow for Railway Network Delay Optimizer - Budibadu
Recommendation Algorithm Flow for Railway Network Delay Optimizer - Budibadu

Best Answers

java
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;
    }
}