-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path1514. Path with Maximum Probability
30 lines (30 loc) · 1.25 KB
/
1514. Path with Maximum Probability
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
class Solution {
public:
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
vector<pair<int,double>>adj[n]; //Creating adjacency list
for(int i=0;i<edges.size();i++){
adj[edges[i][0]].push_back({edges[i][1],succProb[i]});
adj[edges[i][1]].push_back({edges[i][0],succProb[i]});
}
priority_queue<pair<double,int>>pq; //Use maxHeap for path with the maximum probability
pq.push({1.0,start}); //{probability,node}
vector<double>dist(n,INT_MIN);
dist[start]=1;
while(!pq.empty()){
auto itr=pq.top();
pq.pop();
double dis=itr.first;
int node=itr.second;
for(auto it:adj[node]){
int adjNode=it.first;
double edW=it.second;
if(dist[adjNode]<dis*edW){ //If greater probability is found then update probability of adjacent node & push adjacent node in maxHeap
dist[adjNode]=dis*edW;
pq.push({dist[adjNode],adjNode});
}
}
}
if(dist[end]==INT_MIN) return 0.00000; //If there is no path from start to end
else return dist[end];
}
};