-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (40 loc) · 1.43 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succ_prob: List[float], start_node: int,
end_node: int) -> float:
adj = [[] for _ in range(n)]
for i, (a, b) in enumerate(edges):
adj[a].append((b, succ_prob[i]))
adj[b].append((a, succ_prob[i]))
vis = set()
pq = [(-1, start_node)]
while len(pq) > 0:
neg_prob, u = heapq.heappop(pq)
if u in vis:
continue
vis.add(u)
if u == end_node:
return -neg_prob
for v, p in adj[u]:
if v in vis:
continue
heapq.heappush(pq, (neg_prob * p, v))
return 0
class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succ_prob: List[float], start_node: int,
end_node: int) -> float:
adj = [[] for _ in range(n)]
for i, (a, b) in enumerate(edges):
adj[a].append((b, succ_prob[i]))
adj[b].append((a, succ_prob[i]))
vis = [False] * n
pq = [(-1, start_node)]
while len(pq) > 0:
nprob, u = heapq.heappop(pq)
if u == end_node:
return -nprob
if vis[u]:
continue
vis[u] = True
for v, p in adj[u]:
heapq.heappush(pq, (nprob * p, v))
return 0