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
| class Item { public: int node, skip, dis; Item(int a, int b, int c):node(a),skip(b),dis(c){} bool operator>(const Item& R) const { return this->dis > R.dis; } }; class Solution { public: int INF = 0x3f3f3f3f; int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) { vector<vector<pair<int, int>>> g(n); for (auto& e : flights) g[e[0]].push_back({ e[1], e[2] }); vector<int> dist(n, INF); dist[src] = 0; priority_queue<Item, vector<Item>, greater<Item>> pq; pq.emplace(src, 0, 0); while (!pq.empty()) { auto cur = pq.top(); pq.pop(); int node = cur.node; int dis = cur.dis; int skip = cur.skip; if (skip > k) continue; for (auto& e : g[node]) { int w_node = e.first; int w_weight = e.second; if (dist[w_node] > dist[node] + w_weight) { dist[w_node] = dist[node] + w_weight; pq.emplace(w_node, skip + 1, dist[w_node]); } } } return dist[dst]; } };
|