Path with Maximum Probability
Real-world version: Most Reliable Parcel RouteThe statement here is our own: the same inputs, outputs and technique.
A courier company runs n depots, numbered 0 to n - 1, joined by two-way trunk links.
The links are given as two parallel arrays. links[i] = [u, v] is a link that a parcel may cross in either direction, and success[i] is the probability that a parcel crossing that link arrives undamaged. No pair of depots is joined by more than one link, and no link joins a depot to itself.
A route is a sequence of links leading from one depot to the next. A parcel survives the route only if it survives every link on it, and the links fail independently, so the route's success probability is the product of the success values of the links it uses.
Return the largest success probability of any route from depot origin to depot target. If no route joins them at all, return 0.0.
Report the probability as a decimal number; an answer within 1e-5 of the correct value is accepted.
Examples
Example 1
- Input
- n = 4, links = [[0,1],[1,3],[0,2],[2,3]], success = [0.5,0.75,0.875,0.25], origin = 0, target = 3
- Output
- 0.375
The route 0 to 1 to 3 uses the links with chances 0.5 and 0.75, so a parcel survives it with probability 0.5 * 0.75 = 0.375. The other route, 0 to 2 to 3, multiplies out to 0.875 * 0.25 = 0.21875.
Example 2
- Input
- n = 5, links = [[0,1],[1,2],[3,4]], success = [0.5,0.5,1.0], origin = 0, target = 4
- Output
- 0.0
Depots 0, 1 and 2 sit on one side and depots 3 and 4 on the other, with no link crossing between the two sides, so no route reaches depot 4 from depot 0.
Example 3
- Input
- n = 2, links = [[0,1]], success = [0.625], origin = 1, target = 0
- Output
- 0.625
The single link is written as [0,1] but may be crossed in either direction, so the one-link route from depot 1 to depot 0 carries its chance of 0.625.
Constraints
- 2 <= n <= 10^4
- 0 <= links.length <= 2 * 10^4
- links.length == success.length
- links[i].length == 2
- 0 <= links[i][j] <= n - 1
- links[i][0] != links[i][1]
- 0 <= success[i] <= 1
- 0 <= origin <= n - 1
- 0 <= target <= n - 1
- origin != target
- At most one link joins any given pair of depots.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def best_delivery_odds(n: int, links: list[list[int]], success: list[float], origin: int, target: int) -> float:public double bestDeliveryOdds(int n, int[][] links, double[] success, int origin, int target)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Minimum Cost to Make at Least One Valid Path in a GridReal-world version: Cheapest Rewiring Of Conveyor ArrowsHard
- Network Delay TimeReal-world version: Relay Broadcast TimeMedium
- Find a Safe Walk Through a GridReal-world version: Shielded Crossing of a Scorched Bay FloorMedium
- Network Recovery PathwaysReal-world version: Fibre Restoration GradeHard