Graph Theory37 sections · 1633 units
Open in Course

LeetCode 1514 Path with Maximum Probability - Modifying Dijkstra

Different metrics and objectives

Dijkstra is usually for "Minimum Sum". Can you use it for "Maximum Product"? Yes! The greedy logic still holds. You want to pick the "best" path so far. Changes:

1.1. dist array becomes prob array. Initialize to 00 (or 1-1).

2.2. prob[start] = 1.01.0 (100100% chance to start).

3.3. Priority Queue should return the Largest value (Max-Heap). (So use the default priority_queue, no greater needed).

4.4. Relaxation: if (prob[u] * w > prob[v]) prob[v] = prob[u] * w;