City Tour
Time limit2sMemory limit512 MB
Find the minimum and maximum time of a tour from city 1 that visits every city once and uses each transportation means exactly once.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Albert plans to visit cities during his vacation. For convenience, the cities are numbered 1 to . The cities are connected by various transportation means such as trains, airplanes, cruise ships, and buses. There are also different transportation means, numbered 1 to for convenience.
is the time it takes to travel from city to city using the -th transportation means. If this value is 0, the means cannot be used to travel from to . It is always true that .
For example, let , , , and .
- , so traveling from city 1 to city 2 by the 1st means takes 1 unit of time.
- , so traveling from city 3 to city 1 by the 2nd means takes 2 units of time.
- , so traveling from city 2 to city 3 by the 3rd means takes 4 units of time.
While looking into various transportation options and schedules, Albert wanted to know the shortest and longest times for a route that starts at city 1, uses each transportation means exactly once, visits each city exactly once, and then returns to city 1.
In the example above, consider the route city 1 -> city 2 -> city 3 -> city 1.
- Using the means in the order 1, 2, 3 takes units of time.
- Using the means in the order 1, 3, 2 takes units of time. This is the shortest route in this example.
- Using the means in the order 2, 1, 3 takes units of time. This is the longest route in this example.
- Other orders also let Albert use each means once, visit each city once, and return.
For this example, the answers are 7 and 14. Given and the 3D array , find the minimum and maximum times for such a tour.
Input
The first line contains the number of test cases .
Each test case starts with a line containing . The next lines each contain integers separated by spaces. The first lines give , where the first of these lines is for , the next is for , and so on. The next lines give the times for the 2nd means, and this continues in order up to the -th means.
Output
For each test case, print the minimum and maximum time on one line, separated by a space. If no tour satisfies the conditions, print "0 0".
Constraints
- For all :
- For all : is guaranteed.
- For all : is guaranteed.