<p>There are <code>n</code> cities connected by some number of flights. You are given an array <code>flights</code> where <code>flights[i] = [from<sub>i</sub>, to<sub>i</sub>, price<sub>i</sub>]</code> indicates that there is a flight from city <code>from<sub>i</sub></code> to city <code>to<sub>i</sub></code> with cost <code>price<sub>i</sub></code>.</p>

<p>You are also given three integers <code>src</code>, <code>dst</code>, and <code>k</code>, return <em><strong>the cheapest price</strong> from </em><code>src</code><em> to </em><code>dst</code><em> with at most </em><code>k</code><em> stops. </em>If there is no such route, return<em> </em><code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png" style="width: 332px; height: 392px;" />
<pre>
<strong>Input:</strong> n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
<strong>Output:</strong> 700
<strong>Explanation:</strong>
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png" style="width: 332px; height: 242px;" />
<pre>
<strong>Input:</strong> n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
<strong>Output:</strong> 200
<strong>Explanation:</strong>
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png" style="width: 332px; height: 242px;" />
<pre>
<strong>Input:</strong> n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
<strong>Output:</strong> 500
<strong>Explanation:</strong>
The graph is shown above.
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>0 &lt;= flights.length &lt;= (n * (n - 1) / 2)</code></li>
	<li><code>flights[i].length == 3</code></li>
	<li><code>0 &lt;= from<sub>i</sub>, to<sub>i</sub> &lt; n</code></li>
	<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
	<li><code>1 &lt;= price<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
	<li>There will not be any multiple flights between two cities.</li>
	<li><code>0 &lt;= src, dst, k &lt; n</code></li>
	<li><code>src != dst</code></li>
</ul>

class Solution {
public:
    // flights: vector of city/flight pairs: <from, to, price>
    // we want to travel from src, to dst, with <= k stops with the cheapest price
    // return -1 if no such route

    // optimal graph search problem: dijkstras? with modification to terminate searches > k stops.

    // 1: convert flights into unordered map node->(node,weight)
    // 2: call search()
    // 3: return result of search

    int search(int n, int currNode, int des, int k, unordered_map<int,vector<pair<int,int>>>& graph) {
        // create minHeap to store best current search (store current node, cost to get here, steps to get here)
        priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> minHeap;
        // push current position onto heap
        int currCost = 0;
        int currSteps = 0;
        minHeap.push({currCost, currNode, currSteps}); // {cost, node, steps}

        // create vector/set to save optimal paths to nodes we've already visited (avoid redundant searches)
        vector<vector<int>> track(n, vector<int>(k+2, INT_MAX));

        //while heap isn't empty
        while (!minHeap.empty()) {
            // grab item off heap (best candidate for optimal path)
            vector<int> curr = minHeap.top();
            minHeap.pop();
            currCost = curr[0];
            currNode = curr[1];
            currSteps = curr[2];
            
            // Iterate over nodes connected
            for (const auto& [nextNode, costToNext] : graph[currNode]) {
                // check to ensure we haven't already found a better path to this next node
                // also check k step condition
                // Note: might need to store number of steps in track vector. 
                // if we find same cost but less steps, we should prioritize less steps

                // push onto minHeap with total new cost
                if ((currSteps <= k) && (track[nextNode][currSteps+1] > currCost + costToNext)) {
                    track[nextNode][currSteps+1] = currCost + costToNext;
                    minHeap.push({currCost + costToNext, nextNode, currSteps + 1});
                }
            }
        }

        int minPath = INT_MAX;
        for (const auto& path : track[des]) {
            minPath = min(minPath, path);
        }
        return minPath;
    }
    int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
        // 1: convert flights into unordered map node->(node,weight)
        unordered_map<int, vector<pair<int,int>>> graph;
        for (const auto& flight : flights) {
            graph[flight[0]].push_back({flight[1], flight[2]});
        }

        // 2: call search()
        int result = search(n, src, dst, k, graph); 

        // 3: return result of search, checking feasibility   
        if (result == INT_MAX) {
            return -1;
        }  
        return result;
    }
};
22125