| 1 | class Solution: |
| 2 | def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float: |
| 3 | adj = collections.defaultdict(list) |
| 4 | for i in range(len(edges)): |
| 5 | src, dst = edges[i] |
| 6 | adj[src].append([dst, succProb[i]]) |
| 7 | adj[dst].append([src, succProb[i]]) |
| 8 | |
| 9 | pq = [(-1, start)] |
| 10 | visit = set() |
| 11 | |
| 12 | while pq: |
| 13 | prob, cur = heapq.heappop(pq) |
| 14 | visit.add(cur) |
| 15 | |
| 16 | if cur == end: |
| 17 | return prob * -1 |
| 18 | for nei, edgeProb in adj[cur]: |
| 19 | if nei not in visit: |
| 20 | heapq.heappush(pq, (prob * edgeProb, nei)) |
| 21 | return 0 |