You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i].
Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability.
If there is no path from start to end, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5.
Example 1:
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
Output: 0.25000
Explanation: There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.
Example 2:
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2
Output: 0.30000
Example 3:
Input: n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
Output: 0.00000
Explanation: There is no path between 0 and 2.
Constraints:
2 <= n <= 10^4
0 <= start, end < n
start != end
0 <= a, b < n
a != b
0 <= succProb.length == edges.length <= 2*10^4
0 <= succProb[i] <= 1
There is at most one edge between every two nodes.
class Solution:
    def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float:
        # undirected graph
        # weighted positive edges
        # single start
        
        # shortest path <-> longest path
        
        # 最短路 正边权 稀疏图 -》 stack dijkstra

        N = 100010
        dist = [2 for i in range(N)] 
        seen = set()
        
        h = [-1 for i in range(N)]
        e = {}
        ne = {}
        w = {}
        idx = 0
        
        # build singly linked list graph
        def add(a, b, c, idx):
            e[idx], w[idx] = b, c
            ne[idx] = h[a] 
            h[a] = idx
            
        for edge, prob in zip(edges, succProb):
            add(edge[0], edge[1], prob, idx)
            idx += 1
            add(edge[1], edge[0], prob, idx)
            idx += 1
        
        
        def heap_dijkstra():
            dist[start] = 1
            heap = []
            heapq.heappush(heap, (-1, start))

            while heap:
                t = heapq.heappop(heap)
                vertex, distance = t[1], -t[0]
                if vertex in seen:
                    continue
                    
                seen.add(vertex)
                    
                i = h[vertex] # idx
                while i != -1:
                    j = e[i]
                    if dist[j] == 2 or dist[j] < distance * w[i]:
                        dist[j] = distance * w[i]
                        heapq.heappush(heap, (-dist[j], j))

                    i = ne[i]
            return dist[end]
        
        '''
        graph = [[1e-6 for i in range(N)] for j in range(N)]

        for edge, prob in zip(edges, succProb):
            graph[edge[0]][edge[1]] = max(prob, graph[edge[0]][edge[1]])   
            graph[edge[1]][edge[0]] = max(prob, graph[edge[1]][edge[0]])            
        
        def dijkstra():
            dist[start] = 1
            
            for i in range(n):
                t = -1
                for j in range(n):
                    if not seen[j] and (t == -1 or dist[t] < dist[j]):
                        t = j
                
                seen[t] = True
                for j in range(n):
                    dist[j] = max(dist[j], dist[t] * graph[t][j])
            
            return dist[end]
        
        '''
        

        
        output = heap_dijkstra()

        if output == 2 or output < 1e-5:
            return 0.0
        else:
            return output
class Solution:
    def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float:
        # undirected graph
        # weighted positive edges
        # single start
        
        # shortest path <-> longest path
        
        # 最短路 正边权 稀疏图 -》 stack dijkstra

        N = 100010
        dist = [2 for i in range(N)] 
        seen = set()
        
        h = [-1 for i in range(N)]
        e = {}
        ne = {}
        w = {}
        idx = 0
        
        # build singly linked list graph
        def add(a, b, c, idx):
            e[idx], w[idx] = b, c
            ne[idx] = h[a] 
            h[a] = idx
            
        for edge, prob in zip(edges, succProb):
            add(edge[0], edge[1], prob, idx)
            idx += 1
            add(edge[1], edge[0], prob, idx)
            idx += 1
        
        
        def heap_dijkstra():
            dist[start] = 1
            heap = []
            heapq.heappush(heap, (-1, start))

            while heap:
                t = heapq.heappop(heap)
                vertex, distance = t[1], -t[0]
                if vertex in seen:
                    continue
                    
                seen.add(vertex)
                    
                i = h[vertex] # idx
                while i != -1:
                    j = e[i]
                    if dist[j] == 2 or dist[j] < distance * w[i]:
                        dist[j] = distance * w[i]
                        heapq.heappush(heap, (-dist[j], j))

                    i = ne[i]
            return dist[end]
        
        '''
        graph = [[1e-6 for i in range(N)] for j in range(N)]

        for edge, prob in zip(edges, succProb):
            graph[edge[0]][edge[1]] = max(prob, graph[edge[0]][edge[1]])   
            graph[edge[1]][edge[0]] = max(prob, graph[edge[1]][edge[0]])            
        
        def dijkstra():
            dist[start] = 1
            
            for i in range(n):
                t = -1
                for j in range(n):
                    if not seen[j] and (t == -1 or dist[t] < dist[j]):
                        t = j
                
                seen[t] = True
                for j in range(n):
                    dist[j] = max(dist[j], dist[t] * graph[t][j])
            
            return dist[end]
        
        '''
        

        
        output = heap_dijkstra()

        if output == 2 or output < 1e-5:
            return 0.0
        else:
            return output