Lumbo Jumbo
Time limit2sMemory limit512 MB
Given a 3 by N weighted grid and P important cells, find the minimum total edge cost of a walk that visits every important cell at least once.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph, Minimum spanning tree
- Solved
- No attempts yet
Problem
In Lala land, young Lumbo and his older brother Jumbo used to play a game they loved. The original game went like this. Jumbo picks a number and draws a grid on the floor, marking some cells as important. A cell is written , where is the row number (row 1 is the top row) and is the column number (column 1 is the leftmost column). The grid has 3 rows and columns. Jumbo then asks Lumbo to jump on the grid cells with one leg, following these rules:
- Lumbo can start from any cell he chooses.
- Every important cell must be visited at least once.
- Lumbo can jump from one cell to another only if the two cells are adjacent. Two cells are adjacent if they share at least one edge.
- Lumbo may visit non-important cells as many times as he wants. Otherwise the game could be impossible.
One day Jumbo decided the game was too easy, so he added a new rule:
- Jumbo assigns a cost to each edge between two adjacent cells. To cross an edge, Lumbo pays its cost. Once Lumbo has paid for an edge, he can use it as many times as he wants without paying again.
Jumbo asks Lumbo to jump again under all the rules above, visiting each important cell at least once, and this time to minimize the total cost.
Lumbo tried several jumps but is not sure whether any of them is the cheapest. Help him compute the minimum cost.
Input
The first line contains a single integer , the number of test cases ().
Each test case starts with a line containing two integers and . is the number of columns (), and is the number of important cells ().
The next 3 lines each contain integers. The first line gives the costs of the vertical edges in row 1, that is, between and , up to between and . The second and third lines give the same for rows 2 and 3.
The next 2 lines each contain integers. The first line gives the cost of the edge between and for each column . The second line gives the cost of the edge between and for each column .
Every edge cost is between 0 and 100 inclusive ( edge cost ).
The last lines each contain two integers and , the row and column of an important cell (, ).
Output
For each test case, print one line with the minimum cost Lumbo must pay to visit every important cell at least once.
Hint
The figure below explains the second test case.
