UVa 116 - Unidirectional TSP

116 - Unidirectional TSP

 * http://acm.uva.es/p/v1/116.html

Summary
A standard Dynamic Programming problem.

Explanation
Let $$f(x,y)$$ be the optimal (minimum) sum that ends at grid point x and y, and $$v(x,y)$$ be the value of grid point x and y. Then $$f(x,y)$$ is simply


 * $$f(x,y) = min( f(x-1,y-1), f(x,y-1), f(x+1,y-1) ) + v(x,y)$$

Assuming that $$cost[i][j]$$ represents the minimal cost of reaching $$node[i][j]$$ from any of the entry rows at column 1, transforming the recurrence to DP is straightforward:
 * $$cost[i][j] = min(cost[i - j][j - 1], cost[i][j - 1], cost [i + 1][j - 1]) \ldots \mbox{(1)}$$

With above recurrence, the shortest path cost of the entire graph is the least minimal cost of reaching an exit point relative to the minimal costs of reaching all the other graph exit points. More formally, we acquire the best possible exit point, and thus the graph shortest path (minimal cost) by:
 * $$\mbox{graph minimal cost} = min(cost[1][cols], cost[2][cols], \cdots, cost[rows][cols]) \ldots \mbox{(2)}$$

Given above equations, the shortest path vertices (rows) can also be built on each step, and formed from right to left. In the discussion below, we will call the $$min$$ operator at $$(1)$$ our comparison operator.

There's an extra constraint that can make the direct calculations at $$(1)$$ and $$(2)$$ cumbersome: natural order. If two paths costs proved equal, the problem asks for preferring the path with the smaller vertices values (row indices) from left to right. For example, with calculation at $$(1)$$, if $$cost[i][j - 1]$$ is the minimal value given, and $$cost[i][j - 1] = cost[i - j][j - 1]$$, then we will have to re-construct the sub-paths leading to each parameter respective node (namely $$node[i - 1][j - 1]$$ and $$node[i][j - 1]$$) and pick the smallest by comparing those sub-paths vertices values (rows) from left to right.

This irritation comes from the fact of building paths beginning from exit points on the far right and moving left, while having the need to compare completed sub-component sub-paths from left to right. i.e., our path-building comparison operator moves from right to left, while we want to order cost-equivalent sub-paths from left to right.

There's a neat trick to 'unite' the direction of the comparison operator and the natural-order sub-paths comparisons: inverting the grid iteration direction by letting the exit points be on the left, and entry ones on the right.

Due to the dynamic programming nature of the algorithm, we will still build the paths from the exit points, but they will already be on left, where we will be able to compare equivalent sub-paths (in a natural order fashion) neatly in the path building process itself (with no need for re-construction), guaranteeing a clear algorithm with no ugly special cases.

Gotcha's

 * The example given in the problem statement falsely implies as if a greedy algorithm will work
 * 'The sum wont exceed 30-bits' is meaningless as an indication for array values size since negative values are allowed in the summation components
 * Each row may not be in an input line of its own
 * If there is more than one path of minimal weight, the path that is lexicographically smallest should be output.
 * 'Lexicographically' in this problem context means the natural order on sequences induced by the order of their elements (minimize the row values in natural order)
 * In output, row numbers are required to begin from $$1$$

Implementations
Implement row wrapping using the modulo operator instead of using if..else constructs.

Input
5 6 3 4 1 2 8 6 6 1 8 2 7 4 5 9 3 9 9 5 8 4 1 3 2 6 3 7 2 8 6 4 5 6 3 4 1 2 8 6 6 1 8 2 7 4 5 9 3 9 9 5 8 4 1 3 2 6 3 7 2 1 2 3 2 2 9 10 9 10 12 14 1 2 2 1 1 1 1 1 1 1 1 1 1 2 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 4 7 1 2 -3 4 -2 1 5 -1 3 5 -2 6 -3 4 2 1 3 -2 -1 3 1 3 -3 4 2 -3 4 3 5 6 3 4 1 2 8 6 6 1 8 2 7 4 5 9 3 9 9 5 8 4 1 3 2 6 3 7 2 8 6 4 5 6 3 4 1 2 8 6 6 1 8 2 7 4 5 9 3 9 9 5 8 4 1 3 2 6 3 7 2 1 2 3 2 2 9 10 9 10 5 6 1 1 1 1 1 1 2 2 2 2 2 2 3 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