forked from shruti170901/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheapestFlightsWithinKStops.cpp
71 lines (61 loc) · 1.74 KB
/
CheapestFlightsWithinKStops.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
//DFS
#if 0
class Solution {
public:
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K) {
g_.clear();
for (const auto& e : flights)
g_[e[0]].emplace_back(e[1], e[2]);
vector<int> visited(n, 0);
visited[src] = 1;
int ans = INT_MAX;
dfs(src, dst, K + 1, 0, visited, ans);
return ans == INT_MAX ? - 1 : ans;
}
private:
unordered_map<int, vector<pair<int,int>>> g_;
void dfs(int src, int dst, int k, int cost, vector<int>& visited, int& ans) {
if (src == dst) {
ans = cost;
return;
}
if (k == 0) return;
for (const auto& p : g_[src]) {
if (visited[p.first]) continue; // do not visit the same city twice.
if (cost + p.second > ans) continue; // IMPORTANT!!! prunning
visited[p.first] = 1;
dfs(p.first, dst, k - 1, cost + p.second, visited, ans);
visited[p.first] = 0;
}
}
};
#endif
//BFS
class Solution {
public:
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K) {
unordered_map<int, vector<pair<int,int>>> g;
for (const auto& e : flights)
g[e[0]].emplace_back(e[1], e[2]);
int ans = INT_MAX;
queue<pair<int,int>> q;
q.push({src, 0});
int steps = 0;
while (!q.empty()) {
int size = q.size();
while (size--) {
int curr = q.front().first;
int cost = q.front().second;
q.pop();
if (curr == dst)
ans = min(ans, cost);
for (const auto& p : g[curr]) {
if (cost + p.second > ans) continue; // Important: prunning
q.push({p.first, cost + p.second});
}
}
if (steps++ > K) break;
}
return ans == INT_MAX ? - 1 : ans;
}
};