<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:
    unordered_map<int,vector<pair<int,int>>> network;
    int price = 0;
    int res = 10000000;
    unordered_map<int,vector<pair<int,int>>> stop_price;
    inline bool continueroute(int city, int stop, int price){
        if (stop_price.find(city) == stop_price.end()){
            //the first route for the city found, add it to hash map and return true
            pair<int,int> p = {stop, price};
            vector<pair<int,int>> v;
            v.emplace_back(p);
            stop_price.emplace(city,v);
            return true;
        } else{
            auto it = find_if(stop_price[city].begin(), stop_price[city].end(), 
            [&stop](const pair<int,int>& p){ return p.first == stop; });
            if (it == stop_price[city].end()){
                //if there exist a route that has smaller stops and lower / same price with this route, return false 
                for (int i=0; i< stop_price[city].size();i++){
                    if (stop_price[city][i].first < stop && stop_price[city][i].second <= price ){
                        return false;
                    }
                }
                pair<int,int> p = {stop, price};
                stop_price[city].emplace_back(p);
                return true;
            } else{//route with same stops already exist, if the price is lower than existing route, update the price and return true
                if (price < it-> second){
                    it->second = price;  
                    return true;
                } else{ //price higher, return false and not continue with this route
                    return false;
                }
            }
        }

    }
    void dfs(int city, int dest, int k,int stop){                 
        //cout << "depart " << city << endl;
        if (city == dest){
            //cout << "reached " << price << endl;
            res = min(res,price);
            return;
        }
        for (int i=0; i< network[city].size();i++){
            int next = network[city][i].first;
            //cout << "next " << next << endl;
            if (stop<=k && price < res-network[city][i].second){//visit next city
                price += network[city][i].second;
                //
                if ( continueroute(next, stop+1, price) ){
                    dfs(next, dest,k,stop+1);
                }               
                price -= network[city][i].second;
            }
        }
        //cout << city << "," << price << endl;
    }

    int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
        for (int i=0; i< flights.size();i++){
            if (network.find(flights[i][0]) == network.end()){
                vector<pair<int,int>> v;
                pair<int,int> p= {flights[i][1],flights[i][2]}; //destination & price
                v.emplace_back(p);
                network.emplace(flights[i][0],v);
            } else{
                pair<int,int> p= {flights[i][1],flights[i][2]}; //destination & price
                network[flights[i][0]].emplace_back(p);
            }

        }

        dfs(src, dst,k,0);
        if (res == 10000000){
            return -1;
        }
        return res;
    }
};
19155