KatInterview

Shortest Path Through a Network With Hazardous Nodes

Trees & GraphsAuto-graded · 21 test cases
MediumAuto-gradedTrees & Graphsbfsdijkstrashortest-path

Last updated

Tests BFS on filtered graphs and Dijkstra with lexicographic two-criteria cost to navigate networks while minimizing hazardous-node visits then total hops.

Shortest Path Through a Network With Hazardous Nodes

You are given a network of n stations labeled 0 through n-1. Connectivity is described by a list of lines, where each line is an ordered sequence of stations. Any two stations that appear consecutively in the same line are directly connected (the connection is bidirectional and unweighted).

Some stations are marked hazardous. You must solve two navigation problems on this network.

Part 1 — Hazard-Free Routing

Find the fewest hops to travel from origin to destination while never passing through a hazardous station.

  • If origin or destination is itself hazardous, the query is invalid: return -1.
  • If no hazard-free path exists, return -1.

Part 2 — Optimal Routing With Hazards Allowed

You may now travel through hazardous stations. Among all paths from origin to destination, choose one that is best under the following priority:

  1. Primary: minimize the number of hazardous stations visited along the path (including origin and destination if they are hazardous).
  2. Secondary: among all paths tied on criterion 1, minimize the total number of hops.

Return the hazard count and hop count for this optimal path.

Return Format

Return a list of two lists:

[[part1_hops], [part2_hazards, part2_hops]]
  • part1_hops is the hazard-free hop count, or -1 if Part 1 has no valid answer.
  • part2_hazards and part2_hops are both -1 if destination is unreachable even when hazards are permitted.

Input

ParameterTypeDescription
nintNumber of stations (labeled 0..n-1)
linesList[List[int]]Each inner list is a sequence of connected stations
originintStarting station
destinationintTarget station
hazardousList[int]Stations considered hazardous

Constraints

  • 1 <= n <= 500
  • 0 <= len(lines) <= 200
  • Each line contains between 1 and n station labels (all in range [0, n-1])
  • 0 <= origin, destination < n
  • origin may equal destination
  • 0 <= len(hazardous) <= n
  • Station labels in hazardous are all in range [0, n-1]; no duplicates
  • The same pair of stations may appear as neighbors in more than one line (parallel edges are harmless)
  • A line with a single station produces no edges

Examples

Example 1

Input: navigate_hazardous_network(5, [[0, 1, 2], [0, 3, 4, 2]], 0, 2, [1, 3])

Output: [[-1], [1, 2]]

Example 2

Input: navigate_hazardous_network(4, [[0, 1], [1, 2], [2, 3]], 0, 3, [1, 2])

Output: [[-1], [2, 3]]

Example 3

Input: navigate_hazardous_network(3, [[0, 1]], 0, 2, [])

Output: [[-1], [-1, -1]]

Hints

Hint 1

For Part 1, think of it as BFS on a stripped-down graph: simply never enqueue a neighbor that appears in the hazardous set, and reject the query immediately if either endpoint is hazardous.

Hint 2

For Part 2, a single numeric cost won't capture both criteria at once. Consider representing the cost of reaching a node as a tuple (hazards_so_far, hops_so_far) — Python compares tuples lexicographically, which matches the required priority order exactly.

Hint 3

In Part 2, a node can legitimately be reached more than once if a later path finds fewer hazards (or the same hazards in fewer hops). Track the best known cost tuple per node and only expand a state when its cost still matches that best — this is the standard Dijkstra stale-entry check.