-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1514.cpp
44 lines (36 loc) · 1 KB
/
1514.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
#include<bits/stdc++.h>
using namespace std;
typedef pair<double, int> P;
double maxProbability(int n, vector<vector<int>>& edges,vector<double>& succProb, int start, int end) {
unordered_map<int, vector<pair<int, double>>> adj;
vector<P> res(n,0.0);
// made the required vector called adjacent
for(int i =0; i < edges.size(); i++){
int u = edges[i][0];
int v = edges[i][1];
double w = edges[i];
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
// djikstra algo
priority<P> pq; // max heap
res[start_node] = 1.0;
pq.push({1.0, start_node});
while(!pq.empty()){
double currProb = pq.top().first;
int currNode = pq.top().second;
pq.pop();
for(const auto & child : adj[currNode]){
int edgeNode = child.second;
double edgeProb = child.first;
if(edgeProb * currProb > res[edgeNode]){
res[edgeNode] = edgeProb * currProb;
pq.push({res[edgeNode], edgeNode});
}
}
return res[end_node];
}
}
int main(){
return 0;
}