Problem 510438 · medium · Phase 05 Advanced Algorithms & Graphs

Most Reliable Connection

graphs · Dijkstra · max-heap · probability

A sensor network has n nodes 0 .. n-1 joined by undirected links. links[i] = [u, v, p] says the link between u and v delivers a message with probability p (a float, 0 <= p <= 1). Links fail independently, so a message that travels along a path arrives with probability equal to the product of the link probabilities.

Return the highest arrival probability of any path from src to dst. Return 0.0 if dst cannot be reached and 1.0 if src == dst. Answers within 1e-6 are accepted.

Examples

Input:  n = 3, links = [[0,1,0.5],[1,2,0.5],[0,2,0.2]], src = 0, dst = 2
Output: 0.25
Explanation: 0 -> 1 -> 2 gives 0.5 * 0.5 = 0.25, better than the direct link (0.2).

Input:  n = 3, links = [[0,1,0.5]], src = 0, dst = 2
Output: 0.0

Constraints

  • 1 <= n <= 5000, 0 <= len(links) <= 10**4
  • Target O(E log V) time.

Goals

  • Turn a product-maximisation into a Dijkstra-style search
  • Use a max-heap by negating keys
  • Handle the unreachable case with probability 0
Starting Python…