-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1514. Path with Maximum Probability.cpp
49 lines (39 loc) · 1.22 KB
/
1514. Path with Maximum Probability.cpp
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
typedef pair<double,int> pi;
typedef pair<int,double> pii;
class Solution {
public:
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start_node, int end_node) {
// make an adj list
vector<pii> adj[n];
for(int i=0;i<edges.size();i++)
{
int node1=edges[i][0];
int node2=edges[i][1];
adj[node1].push_back({node2,succProb[i]});
adj[node2].push_back({node1,succProb[i]});
}
// go for dijkstra algorithm
vector<double> dis(n,-1);
priority_queue<pi> pq;
dis[start_node]=1;
pq.push({1,start_node});
while(!pq.empty())
{
auto it=pq.top();
pq.pop();
int node=it.second;
double pro=it.first;
for(auto& child:adj[node])
{
if(dis[child.first]<(pro*child.second))
{
dis[child.first]=pro*child.second;
pq.push({dis[child.first],child.first});
}
}
}
return dis[end_node] > 0 ? dis[end_node] : double(0);
}
};
// queue= {0,0}
// dis [0,0,0]