-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathwithMaximumProbability.cpp
More file actions
35 lines (34 loc) · 1.27 KB
/
pathwithMaximumProbability.cpp
File metadata and controls
35 lines (34 loc) · 1.27 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
30
31
32
33
34
35
// Source: https://leetcode.com/problems/path-with-maximum-probability/
// Author: Miao Zhang
// Date: 2021-05-13
/***************************************************************
* max(p1*p2*p3) = max(log(p1*p2*p3)) = max(log(p1)+log(p2)+log(p3))
* = min(-(log(p1)+log(p2)+log(p3)))
***************************************************************/
class Solution {
public:
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
vector<vector<pair<int, double>>> g(n);
for (int i = 0; i < edges.size(); i++) {
double w = -log(succProb[i]);
g[edges[i][0]].emplace_back(edges[i][1], w);
g[edges[i][1]].emplace_back(edges[i][0], w);
}
vector<double> dist(n, FLT_MAX);
priority_queue<pair<double, int>> q;
q.emplace(-0.0, start);
vector<int> seen(n);
while (!q.empty()) {
double d = -q.top().first;
int cur = q.top().second;
q.pop();
seen[cur] = 1;
if (cur == end) return exp(-d);
for (auto& [nxt, w]: g[cur]) {
if (seen[nxt] || d + w > dist[nxt]) continue;
q.emplace(-(dist[nxt] = d + w), nxt);
}
}
return 0;
}
};