1. There are N cities connected by M flights.
2. Each fight starts from a source city i and arrives at destination city j with a price w.
3. You are given a 2D array representing the flights. The value at index (i, j) of the 2D array represents the price of flight from city i to city j.
4. The value 0 at an index (i, j) of the 2D array represents that there is no flight between city i and j.
5. You are also given the src city, the dst city and a number K.
6. You have to find the cheapest price from src to dst with up to K stops. If there is no such route, output -1.
7. For example:
Input:
N = 3,
Flights =
idx 0 1 2
0 0 100 500
1 0 0 100
2 0 0 0
src = 0, dst = 2, K = 1
Output: 200
Explanation:
The price of flight from city 0 to city 1 = 100
The price of flight from city 0 to city 2 = 500
The price of flight from city 1 to city 2 = 100
The cheapest price from city 0 to city 2 with at most 1 stop costs 200 (100 + 100).
8. main takes input from the users.
9. This is a functional problem.
10. You have to complete the findCheapestPrice function. It takes as input N, the flights 2D array, src city, dst city and K. It should return an integer representing the minimum price of flight from src to dst with at most K stops if there is a route, else should return -1.
11. Don't change the code of main.
Input Format
First line takes input N, the number of cities. Next N lines take N space separated integers representing the flights 2D array. Next line take input 3 space separated integers representing the src city, dst city and K respectively. Input is handled for you.
Output Format
Minimum price of flight from src to dst with upto K stops if there's a route, else -1. Output is handled for you.
Constraints
1 <= N <= 100 0 <= K <= N-1
Notice
Try First, Check Solution later
1. You should first read the question and watch the question video.2. Think of a solution approach, then try and submit the question on editor tab.3. We strongly advise you to watch the solution video for prescribed approach.Example
Input
3 0 100 500 0 0 100 0 0 0 0 2 1
Output
200