Sphere Online Judge



SPOJ Problem Set (classical)

101. Fishmonger

Problem code: FISHER

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

Because he is a good business man, 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 certain time, otherwise his fish start to smell.

Input

The first line contains the number of states n and available time t. The first state is the port, the last state is the market. After this line there are n lines with n numbers each, specifying for each state the travel time to the i-th state. This table is terminated with an empty line. The table of the tolls follows in the same format.

n is at least 3 and at most 50. The time available is less than 1000. All numbers are integers.

There are many test cases separated by an empty line. Input terminates with number of states and time equal 0 0.

Output

For each test case your program should print on one line the total amount of tolls followed by the actual travelling time.

Example

Sample input:
4 7
0 5 2 3
5 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

0 0

Sample output:
6 6

This corresponds to the following situation, the connections are labeled with (time, toll):


Added by:Micha³ Czuczman
Date:2004-07-07
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All
Resource:Swiss Olympiad in Informatics 2004

hide comments
2014-08-10 19:51:58 mohsin mohammad
I caught the fish Dynamically!!!!
2014-04-24 20:24:34 Julian Waldby
If two paths have toll the same yet one has a smaller travelling time (and both are within the time limit) should we post the smaller travelling time?
2014-03-21 00:58:05 Jens Stimpfle
You can assume i!=j ==> traveltime[i][j] > 0. Which isn't explicit in the problem description, but makes the problem simpler (maybe even reduces the worst case complexity, but I'm not sure).
2014-01-31 20:41:07 Vipul Pandey
2D dp.
2013-11-20 05:50:51 Aniruddh Kanojia
Approximate Number of test cases ?
2013-09-15 14:27:23 harsh
similar to FPOLICE..:)
2013-08-17 12:26:36 alphaplus



Last edit: 2013-08-17 12:59:42
2013-07-08 02:45:32 samuel
dp with two dimension!!
2013-07-05 06:00:55 Codeblooded
nice dp :)
2012-09-02 06:55:22 rajanya dhar
within a certain time means <7 or <=7?
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.