-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1514-path-with-maximum-probability.cpp
More file actions
29 lines (28 loc) · 1.01 KB
/
1514-path-with-maximum-probability.cpp
File metadata and controls
29 lines (28 loc) · 1.01 KB
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
class 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];
}
};