KatInterview

Bottleneck Path Under an Edge-Weight Cap

Trees & GraphsAuto-graded · 22 test cases
MediumAuto-gradedTrees & Graphsgraphdijkstraminimax

Last updated

Tests whether a candidate can find the minimax-cost path in a filtered graph, recognising that one edge attribute gates inclusion while another drives the optimisation.

Problem

You are given a pipeline network of n stations numbered 0 through n - 1. Stations are connected by bidirectional pipes. Each pipe is described by four integers [a, b, delay, risk]:

  • a and b are the two stations it connects.
  • delay is the time it takes to transfer data through the pipe.
  • risk is the hazard rating of the pipe.

You are also given a starting station src, a target station dst, and an integer max_delay representing the maximum acceptable transfer delay for any single pipe.

A pipe may only be used if its delay is at most max_delay.

The hazard of an entire route is defined as the highest risk value among all pipes used on that route. Your goal is to find a route from src to dst, using only pipes that satisfy the delay constraint, that minimises this hazard.

Return the minimum possible hazard for any such route. If src == dst, return 0. If no valid route exists, return -1.

Constraints

  • 1 <= n <= 10^5
  • 0 <= len(edges) <= 2 * 10^5
  • Each edge is [a, b, delay, risk] where 0 <= a, b < n and a != b
  • 0 <= delay, risk, max_delay <= 10^9
  • 0 <= src, dst < n
  • Multiple pipes between the same pair of stations are allowed

Examples

Example 1

Input: min_required_safety(4, [[0, 1, 10, 5], [1, 2, 8, 9], [0, 2, 15, 7], [2, 3, 5, 4]], 0, 3, 12)

Output: 9

Example 2

Input: min_required_safety(3, [[0, 1, 5, 8], [1, 2, 6, 7]], 0, 2, 4)

Output: -1

Hints

Hint 1

Start by throwing away every pipe whose delay exceeds max_delay — those pipes simply don't exist for this problem. What kind of graph search works on what remains?

Hint 2

The cost of a route is the maximum risk along it, not a sum. Think about how you would adapt Dijkstra's relaxation step: instead of dist[u] + w, what expression gives the cost of extending a path through a new pipe?

Hint 3

A min-heap lets you pop the node with the smallest bottleneck cost first. When the destination is popped, you already have the answer — but this early-exit guarantee only holds with a priority queue, not a plain FIFO queue.