-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution2.cpp
34 lines (34 loc) · 1.01 KB
/
Solution2.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
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) {
int a = edges[i][0], b = edges[i][1];
double s = succProb[i];
g[a].push_back({b, s});
g[b].push_back({a, s});
}
vector<double> d(n);
vector<bool> vis(n);
d[start] = 1.0;
queue<int> q{{start}};
vis[start] = true;
while (!q.empty()) {
int i = q.front();
q.pop();
vis[i] = false;
for (auto& ne : g[i]) {
int j = ne.first;
double s = ne.second;
if (d[j] < d[i] * s) {
d[j] = d[i] * s;
if (!vis[j]) {
q.push(j);
vis[j] = true;
}
}
}
}
return d[end];
}
};