Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 1514. Path with Maximum Probability1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start_node, int end_node) {
vector<vector<pair<int, double>>> adj(n);
vector<double> dist(n, 0); // Initialize distance/probability array with 0
// Build the adjacency list
for (int i = 0; i < edges.size(); i++) {
int u = edges[i][0], v = edges[i][1];
adj[u].emplace_back(v, succProb[i]);
adj[v].emplace_back(u, succProb[i]);
}
priority_queue<pair<double, int>> pq;
pq.push({1, start_node}); // Start with probability 1 for the start node
dist[start_node] = 0; // Probability to reach start node is 1
while (!pq.empty()) {
auto [prob, node] = pq.top(); pq.pop();
if (node == end_node)
return prob;
for (auto& [neighbor, cost] : adj[node]) {
double newProb = prob * cost;
if (newProb > dist[neighbor]) {
dist[neighbor] = newProb;
pq.push({newProb, neighbor});
}
}
}
return 0;
}
};