-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-1514-Path_with_Maximum_Probability.cpp
92 lines (72 loc) · 2.71 KB
/
leetcode-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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
#include <cmath>
double maxProbability(int n, std::vector<std::vector<int>>& edges, std::vector<double>& succProb, int start_node, int end_node) {
std::vector<double> relax;
std::queue<int> need_to_visit;
std::vector<std::vector<std::pair<int, double>>> graph(n);
for (int i = 0; i < edges.size(); ++i) {
graph[edges[i][0]].emplace_back(edges[i][1], succProb[i]);
graph[edges[i][1]].emplace_back(edges[i][0], succProb[i]);
}
need_to_visit.push(start_node);
relax.resize(n, -1);
relax[start_node] = 0;
while(!need_to_visit.empty())
{
for(size_t v = 0; v<graph[need_to_visit.front()].size(); ++v)
{
double node_total = 0;
node_total = relax[need_to_visit.front()];
node_total += -1*(std::log2(graph[need_to_visit.front()][v].second));
if(relax[graph[need_to_visit.front()][v].first] == -1 || relax[graph[need_to_visit.front()][v].first] > node_total)
{
relax[graph[need_to_visit.front()][v].first] = node_total;
need_to_visit.push(graph[need_to_visit.front()][v].first);
}
}
need_to_visit.pop();
}
if(relax[end_node]==-1)
{
return 0;
}
return std::pow(2,(-1*relax[end_node]));
}
int main(){
// int n = 3;
// std::vector<std::vector<int>> edges = {{0,1},{1,2},{0,2}};
// std::vector<double> succProb = {0.5,0.5,0.2};
// int start_node = 0;
// int end_node = 2;
// int n = 5;
// std::vector<std::vector<int>> edges = {{2,3},{1,2},{3,4},{1,3},{1,4},{0,1},{2,4},{0,4},{0,2}};
// std::vector<double> succProb = {0.06, 0.26, 0.49, 0.25, 0.2, 0.64, 0.23, 0.21, 0.77};
// int start_node = 0;
// int end_node = 3;
// int n = 3;
// std::vector<std::vector<int>> edges = {{0,1}};
// std::vector<double> succProb = {0.5};
// int start_node = 0;
// int end_node = 2;
// int n = 5;
// std::vector<std::vector<int>> edges = {{1,4},{2,4},{0,4},{0,3},{0,2},{2,3}};
// std::vector<double> succProb = {0.37,0.17,0.93,0.23,0.39,0.04};
// int start_node = 3;
// int end_node = 4;
// int n = 3;
// std::vector<std::vector<int>> edges = {{0,1},{1,2},{0,2}};
// std::vector<double> succProb = {0.5,0.5,0.2};
// int start_node = 0;
// int end_node = 2;
int n = 3;
std::vector<std::vector<int>> edges = {{0,1},{1,2},{0,2}};
std::vector<double> succProb = {0.5,0.5,0.3};
int start_node = 0;
int end_node = 2;
double res = maxProbability(n, edges, succProb, start_node, end_node);
std::cout << res << std::endl;
return EXIT_SUCCESS;
}