Fishmonger

A fishmonger wants to bring his goods from port to the market. On his route, he has to traverse an area with many states. He has to pay a toll at each border.

He is a good businessman. He wants to choose the route in such a way that he has to pay as little money for tolls as possible. On the other hand, he has to be at the market within a particular time. Otherwise, his fish will start to smell.

You are given ‘N’ number of states and the time ‘M’ in which he has to reach the market. You need to return the minimum toll amount that he has to pay to reach the market in the given time. The 0’th index is the port, and the 'N'-1 index is the market.

For example:
Consider If N = 2 , M = 4, Time = [[0,  2], [3,  0]] and Toll = [[0, 10], [ 5,  0]]]
The optimal path to reach node 1 from node 0 is 0 -> 1. The time required in the path is 2, and the minimum toll needed within the given time constraints is 10. Hence the answer is 10.
Input Format :
The first line contains a single integer ‘T’ representing the number of test cases.

The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, denoting the number of states and given time limit.

The next ‘N’ line contains ‘N’ space-separated integers denoting the elements of the array ‘Time’ where ‘Time[i][j]’ represents the time needed to traverse from state ‘i’ to state ‘j’.

The next ‘N’ line contains ‘N’ space-separated integers denoting the elements of the array ‘Toll’ where ‘Toll[i][j]’ represents the toll needed to traverse from state ‘i’ to state ‘j’.  

Output Format :

For each test case, print the minimum toll needed to reach the market from the port. If we cannot reach the market within the given time limit, then print -1. 

Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
2 <= N <= 50
1 <= M <= 1000
0 <= Toll[i] <= 10^8
0 <= Time[i] <= 1000

Time limit: 1 sec
CodingNinjas
author
2y
Brute Force

In this approach, we have to make a recursive function that returns the minimum toll needed to reach the N-1 node within the given time limit. We have to create a visited array of size N to...read more

CodingNinjas
author
2y
Breadth-First Search

In this approach, we have to maintain a 2d array dist in which dist[i][j] stores the minimum Toll needed to reach index i within the j time limit. We have to make a queue q that we...read more

Help your peers!
Add answer anonymously...
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter