Restock

All submissions for this problem are available.
The chef has to replenish supplies in the kitchen. He divided the kitchen floor into a rectangular grid of cell with N rows and M columns. All coordinates in this problem are 0indexed and in the form (row, column). The supplies will be delivered to the cell (R,C) and have to be moved to the storage, which is located in the cell (0,0). The chef will organize his employees so that they can pass individual items between them from the point of delivery to the storage. However, he has one additional request. The chef wants to see progress with every pass, which means that the Euclidean distance between the item and the storage has to decrease with every pass.
An employee assigned to the cell (Y1,X1) can pass an item to another empoyee in cell (Y2,X2) if Y1Y2 <= D and X1X2 <= D. Note that one person has to be assigned to cell (R,C) in any valid assignment. The chef assigned a wage to each cell to compensate for different working conditions. For example, standing next to an oven is not a very popular spot among the employees. The chef will take the position at the storage himself, therefore the wage for the cell (0,0) will be 0 in all cases. Of course, the chef wants to pay as little as possible. The cost of some assignment of employees to different cells is equal to the sum of wages associated with their cells. The chef can always find some new workforce, so the number of workers is not a restriction. Help him find the cost of the cheapest assignment.
Input
The first line contains a single integer T, the number of test cases. The first line of each testcase contains the number of rows N and the number of columns M. Second line contains integers D, R and C. Following N lines with M integers describe the wages. jth number in ith line represents the wage w_{i1,j1} for the cell (i1,j1).
Output
Output a line with a single integer, the cost of the cheapest assignment of workers to cells such that they can pass the items from the point of delivery to the storage.
Constraints
 1 <= T <= 10
 1 <= N, M <= 500
 1 <= D <= 500
 0 <= w_{i,j} <= 10000
 The sum of N*M over all test cases in a single test file will not exceed 250000.
Example
Input: 2 1 5 2 0 4 0 1 5 1 4 5 6 2 4 3 0 7 8 5 9 1 1 6 8 4 6 2 5 4 2 5 0 3 5 2 0 6 8 8 3 5 3 3 8 4 Output: 6 4
Author:  thocevar 
Tester:  gamabunta 
Editorial  http://discuss.codechef.com/problems/RESTOCK 
Tags  june11, medium, thocevar 
Date Added:  28042011 
Time Limit:  1.28118 sec 
Source Limit:  50000 Bytes 
Languages:  C, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions