|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|1 초||256 MB||5||4||4||80.000%|
Tom is a used popsicle salesman. His recent sales at his stand on Gløshaugen haven’t been very good, so he’s decided that he wants to travel the world selling his merchandise. He has already compiled a list of cities to visit, and have also gathered the costs of the plane trips between them. Now he just have to figure out if he has enough money for the trip. He has of course heard that this travelling salesman problem is really really hard, so the task is down to the smartest people he knows. That’s you (if you don’t feel very smart at the moment, keep in mind that Tom doesn’t know that many people). You need to write a program that computes the lowest cost of Tom visiting all these cities in the given order.
The first line of input contains a single number T, the number of test cases to follow. Each test case begins with a line containing a single number, N, the number of cities Tom will visit. The second line of each test case contains N numbers ai, the order in which Tom wants to visit the N cities. He starts his trip in the first city described, and will travel back there once he has visited the last one. Then follow N lines, each containing N integers, describing the cost cij of travelling from city i to each of the N cities.
For each test case, output a line containing a single number, the lowest possible cost of visiting the cities. If it is not possible to visit all the cities, output impossible instead.
2 3 0 2 1 0 1 2 1 0 1 1 3 0 2 0 1 0 -1 1 0