-
Notifications
You must be signed in to change notification settings - Fork 6
/
cheapest_flights_with_k_stops.cpp
72 lines (62 loc) · 2.79 KB
/
cheapest_flights_with_k_stops.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Solution {
public:
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
// vector<vector<pair<int, int>>> graph(n);
// //using minHeap we keep track of the cheapest price from city x to city y with at most k stops
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>> > minHeap;
// //create adjacency list
// for(vector<int>& flight : flights) {
// int s = flight[0], d = flight[1], dist = flight[2];
// graph[s].push_back({d, dist});
// }
// vector<int> dist(n+1, INT_MAX);
// int e = k + 1; //k = hops; edges = number of hops + 1
// minHeap.push({0, src, e}); // {distanceSoFar, currentNode, edgesSeenSoFar}
// while(!minHeap.empty()) {
// auto current = minHeap.top();
// minHeap.pop();
// int distance = current[0], currentNode = current[1], edges = current[2];
// if(edges < 0) {
// continue;
// }
// if(dist[currentNode] < edges) {
// continue;
// }
// dist[currentNode] = edges;
// if(currentNode == dst) { //if we are at the destination node
// return distance;
// }
// if(edges > 0) { //if not, explore all neighbors of the currentNode in the graph
// for(pair<int, int>& neighbor : graph[currentNode]) {
// minHeap.push({neighbor.second + distance, neighbor.first, edges - 1});
// }
// }
// }
// return -1;
if (flights.empty()) return 0;
vector<unordered_map<int, int>> flights_matrix(n);
for (int i = 0; i < flights.size(); ++i) {
flights_matrix[flights[i][0]][flights[i][1]] = flights[i][2];
}
vector<pair<int, int>> queue;
queue.push_back({src, 0});
unordered_map<int,int> visited_price;
visited_price[src] = 0;
int stops = 0;
while(queue.size() && stops <= k) {
vector<pair<int, int>> new_queue;
for (int i = 0; i < queue.size(); ++i) {
for (const auto& iter : flights_matrix[queue[i].first]) {
const int stop = iter.first;
const int price = queue[i].second + iter.second;
if (visited_price.count(stop) && visited_price[stop] < price) continue;
visited_price[stop] = price;
new_queue.push_back({stop, price});
}
}
swap(queue, new_queue);
++stops;
}
return visited_price.count(dst) == 0 ? -1 : visited_price[dst];
}
};