-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC0787.cpp
More file actions
executable file
·33 lines (28 loc) · 778 Bytes
/
LC0787.cpp
File metadata and controls
executable file
·33 lines (28 loc) · 778 Bytes
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
/*
Problem Statement: https://leetcode.com/problems/cheapest-flights-within-k-stops/
*/
class Solution {
public:
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K) {
vector<vector<pair<int, int>>> adj(n);
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
// initialize
pq.push({0, src, 0});
// construct adjacency list
for (auto& f: flights)
adj[f[0]].emplace_back(f[1], f[2]);
// djikstra's algorithm
while (!pq.empty()) {
int cost, stops;
tie(cost, src, stops) = pq.top();
pq.pop();
if (src == dst)
return cost;
if (stops == K + 1)
continue;
for (auto& [u, w]: adj[src])
pq.push({cost + w, u, stops + 1});
}
return -1;
}
};