FPOLICE - Fool the Police

Dhamaka Singh (a crook) has just robbed a bank and would like to get out of the country as soon as possible. But there is a slight problem, the police! On his way out of the country he has to pass through some police stations. Each police station has a certain risk (for Dhamaka Singh) associated with it. He wants to get to the airport within a certain time T or else he'll miss his flight. He also wants to take a path that minimizes the total risk associated with it. Help Dhamaka Singh get out of the country.


The first line of the input contains an integer t, the number of test cases. t test cases follow.

The first line of each test case contains 2 integers N (3 <= N <= 100) and T (1 <= T <= 250), denoting the number of police stations and the total time he has to reach the airport, respectively.

Dhamaka Singh has to start from the first police station and reach the Nth one (the airport is just after the Nth police station). You can consider the time taken between the Nth police station and the airport to be negligible.

Next there are N lines with N numbers in each line, separated by single spaces. All numbers are separated by a single space. The jth integer in the ith line represents the time taken to reach the jth police station from the ith police station.

Next there are another N lines with N numbers in each line. All numbers are separated by a single space. The jth integer in the ith line represents the risk involved in travelling to the jth police station from the ith police station.


For each test case output one line containing 2 integers separated by a single space.

The first integer denotes the minimum total risk to reach the airport. The second integer denotes the minimum time required to reach the airport at the minimum total risk.

If it is impossible to reach the airport within time T (inclusive), just print "-1" (quotes for clarity).


4 10
0 6 2 3
6 0 2 3
3 1 0 2
3 3 2 0
0 2 2 7
2 0 1 2
2 2 0 5
7 2 5 0

4 9

hide comments
mrolympia: 2014-03-29 08:54:04

AC in first attempt with Dijkstra :)

JordanBelfort: 2014-02-04 18:04:59

nice dp :)

Vipul Pandey: 2014-01-31 20:49:16

same as fisher.

Abhishek Gupta: 2014-01-23 13:44:33

after 8 WA i finally did it with Bellman-Ford...in 0.01 sec :)
I still wonder how it could be done through dijkstra's...

Himank Agrawal: 2013-12-25 16:03:20

can path be like 1->3->2->4 or it should be in increasing order ?

Avaneesh Rastogi: 2013-10-19 12:44:53

For those wondering how the output is 4 9 , the path is
AC(0.02) with recursive top down dynamic programming :)

Last edit: 2013-10-19 15:55:27
coding_express: 2013-05-27 13:11:50

please say how to optimise

Aradhya: 2013-05-27 11:19:57

hell yeah i did this with recursion.. my first recursion handling 2-D arrays :)

Rocker3011: 2012-08-05 18:17:07

do not copy and paste from FISHER... many WA because of that :D!

Rajesh Dhania: 2012-05-29 15:57:40

Can anyone please tell me the complexity of the problem?

Added by:Matthew Reeder
Time limit:1.411s
Source limit:30000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Al-Khawarizm 2006