Consider the i−j plane shown in Fig.1. The discrete-points, or nodes, of interest in this plane are indexed by ordered pairs of positive integers as those points in the first quadrant of the Cartesian plane. The basic problem is to find the least-cost path through the grid, which begins at a designated original node, (1,1), and ends at the designated terminal node, (I,J). Let us define what we mean by a “path” through the grid. A path from node (s,t) to node (u,v) is an ordered set of nodes (i.e., index pairs) of the form:
(s,t),(i1,j1),(i2,j2),(i3,j3),...(u,v), where the intermediate ik,jk’s are monotonic, this means that ik−1≤ik and ji−1≤ji.
Costs are assigned to the nodes in the plane. Let us define the notation dN(i,j) being the cost associated with node (i,j), and Dmin(i,j) for the cost from (1,1) to (i,j) over the best path. Local path constraints are used to restrict the local range of a path in the vicinity of a given node in the grid. In Fig.2, we show the type of local path constraints applied for this problem in terms of sets of predecessor points. The summary of this type of local path constraints, slope weights, and dynamic programming recursion formula are:
Dmin(i,j)=min⎩⎨⎧Dmin(i−2,j−1)+a[dN(i−1,j)+dN(i,j)]Dmin(i−1,j−1)+bdN(i,j)Dmin(i−1,j−2)+a[dN(i,j−1)+dN(i,j)]⎭⎬⎫ And before start, the Dmin(1,1)=DN(1,1).
Consider the problem of finding the best path with least cost through the (I,J) grid of local costs. Assume that the path must begin at (1,1) and end at (I,J). The type of local path constraints shown in Fig.2 is applied. You can assume that the least-cost path **being found is unique**. We guarantee that the I and J does not exceed 20.
Fig. 1
Fig. 2
Input Format
Slope weights a, b, and costs associated with nodes in the grid. Contains J+2 lines.
Line 1: a
Line 2: b
Line 3: dN(1,1) dN(2,1) dN(3,1) ...dN(I,1)
Line 4: dN(1,2) dN(2,2) dN(3,2) ...dN(I,2)
Line 5: dN(1,3) dN(2,3) dN(3,3) ...dN(I,3)
...
Line J+1:dN(1,J−1) dN(2,J−1) dN(3,J−1) ...dN(I,J−1)
Line J+2: dN(1,J) dN(2,J) dN(3,J) ...dN(I,J)
Output Format
The total cost W of the best path, and the ordered set of nodes for the best path. Contains k+1 lines.
Line 1: W
Line 2: (1,1)
Line 3: (i,j)
...
Line k+1: (I,J)
Please accurate to 10−6 for floating number. And there is no space in the coordinates.
样例输入
0.5 1 1 1 3 3 3 3 3 3 3 1 1 1 1 2 2 1 2 2 2 1 2 2 1 3 1 1 1 2 2 2 1 2 2 2 3 3 3 2 3 1 1 1 2 3 3 3 2 3 2 2 2 3 3 3
样例输出
10.000000
(1,1)
(2,2)
(3,3)
(4,4)
(5,4)
(6,5)
(7,5)
(8,6)
(9,6)