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^40 <= start, end < nstart != end0 <= a, b < na != b0 <= succProb.length == edges.length <= 2*10^40 <= succProb[i] <= 1class Solution {
public:
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
vector<vector<pair<int, double>>> adjList(n);
for(int i=0; i<edges.size(); i++){
int u = edges[i][0], v = edges[i][1];
adjList[u].push_back({v, succProb[i]});
adjList[v].push_back({u, succProb[i]});
}
vector<double> prob(n, 0.0); // cost to reach a node (here: probability)
prob[start] = 1.0;
queue <int> q;
q.push(start);
while (!q.empty()){
int cur = q.front();
q.pop();
for(auto nbr: adjList[cur]){
double prob_new = prob[cur] * nbr.second;
if(prob_new > prob[nbr.first]){
prob[nbr.first] = prob_new;
// cout << cur << "---" << nbr.first << "<-" << prob_new << "\n";
q.push(nbr.first);
}
}
}
return prob[end];
}
};class Solution {
private record Node(int idx, double prob) {
}
public double maxProbability(int n, int[][] edges, double[] succProb, int start_node, int end_node) {
List<Node>[] adj = new List[n];
boolean[] vis = new boolean[n];
for (int i = 0; i < n; i++)
adj[i] = new ArrayList<>();
for (int i = 0; i < edges.length; i++) {
adj[edges[i][0]].add(new Node(edges[i][1], succProb[i]));
adj[edges[i][1]].add(new Node(edges[i][0], succProb[i]));
}
return dijkstra(adj, vis, start_node, end_node);
}
private double dijkstra(List<Node>[] adj, boolean[] vis, int src, int dest) {
Queue<Node> pq = new PriorityQueue<>((a, b) -> Double.compare(b.prob, a.prob));
pq.offer(new Node(src, 1d));
while (!pq.isEmpty()) {
Node best = pq.poll();
// System.out.println(best);
vis[best.idx] = true;
if (best.idx == dest)
return best.prob;
for (Node nbr : adj[best.idx])
if(!vis[nbr.idx])
pq.offer(new Node(nbr.idx, best.prob * nbr.prob));
}
return 0d;
}
}