Skip to content

Commit 6b9d15e

Browse files
authored
Cheapest_Flights_Within_K_Stops
1 parent 5907dc5 commit 6b9d15e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Problem Description:- Cheapest_Flights_Within_K_Stops
2+
// Link:- https://leetcode.com/problems/cheapest-flights-within-k-stops/
3+
4+
5+
class Solution {
6+
public:
7+
//Bellman Ford Algorithm
8+
int shortestPath(vector<vector<int>>& flights,int src,int dst,int k,int n){
9+
vector<int> dist(n,INT_MAX);
10+
dist[src] = 0;
11+
for(int count=0; count<=k; count++){
12+
vector<int> temp = dist;
13+
for(vector<int>& v: flights){
14+
if(dist[v[0]] == INT_MAX)
15+
continue;
16+
//Relax Operation
17+
if(temp[v[1]] > dist[v[0]] + v[2])
18+
temp[v[1]] = dist[v[0]] + v[2];
19+
}
20+
dist = temp;
21+
}
22+
return (dist[dst] == INT_MAX ? -1 : dist[dst]);
23+
}
24+
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
25+
return shortestPath(flights,src,dst,k,n);
26+
}
27+
};

0 commit comments

Comments
 (0)