-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_with_max_probability.cpp
44 lines (41 loc) · 1.39 KB
/
path_with_max_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
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
vector<vector<pair<double, int>>> graph(n);
for (int i = 0; i < edges.size(); ++i) {
int from = edges[i][0];
int to = edges[i][1];
double cost = succProb[i];
graph[from].push_back(move(make_pair(cost, to)));
graph[to].push_back(move(make_pair(cost, from)));
}
vector<double> d(n, 0);
using pid = pair<double, int>;
auto compare = [](pid &a, pid &b){return a.first < b.first;};
priority_queue<pid, vector<pid>, decltype(compare)> q(compare);
q.push({start, 1});
for (int i = 0; i < n; ++i) {
// choose the vertex from not marked
int v = q.top().second;
double cost = q.top().first;
q.pop();
if (d[v] != cost)
continue;
for (int j = 0; j < graph[v].size(); ++j) {
int to = graph[v][j].first;
double prob = graph[v][j].second;
if (d[to] < d[v] * prob) {
d[to] = d[v] * prob;
q.push({d[to], to});
}
}
}
return d[end];
}
};
int main() {
return 0;
}