Unidirectional TSP
Description
Background
Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems are minimizing wire length. The Traveling salesperson problem (TSP)--finding whether all the cities in a salesperson ' s route can be visited exactly Once with a specified limit in travel time – is one of the canonical examples of a np-complete problem; Solutions appear to require a inordinate amount of time to generate, but is simple to check.
This problem deals with finding a minimal the path through a grid of points while traveling is from left to right.
The problem
Given a matrix of integers, you is to write a program that computes a path of minimal weight. A path starts anywhere in column 1 (the first column) and consists of a sequence of steps terminating in column n (The last column). A step consists of traveling from column I to column I+1 in an adjacent (horizontal or diagonal) row. The first and last rows (rows 1 and m) of a matrix is considered adjacent, i.e., the matrix ' wraps ' so it Represents a horizontal cylinder. Legal steps is illustrated below.
The weight of a path is the sum of the integers in each of the n cells of the Matrix, which is visited.
For example, the slightly different matrices was shown below (the only difference was the numbers in the bottom row).
The minimal path is illustrated for each matrix. Note the the path for the matrix on the right takes advantage of the Adjacency property of the first and last rows.
The Input
the input consists of a sequence of matrix specifications. Each matrix specification consists of the row and column dimensions in this order on a line followed By integer s where m is the row dimension and n is the column dimension. The integers appear in the input in row major order, i.e., the first n integers constitute the first RO W of the Matrix, the second n integers constitute the second row and so on. The integers on a line would be separated from integers to one or more spaces. Note:integers is not restricted to being positive. There would be the one or more matrix specifications in an input file. Input is terminated by End-of-file.
For each specification the number of rows would be between 1 and inclusive; The number of columns would be between 1 and inclusive. No path ' s weight would exceed integer values representable using the bits.
The Output
The first line represents a minimal-weight pat lines should is output for each of the matrix specification in the input file H, and the second line are the cost of a minimal path. The path consists of a sequence of n integers (separated by one or more spaces) representing the rows that Consti Tute the minimal path. If there is more than one path of minimal weight the path, that's lexicographically smallest should be output.
Sample Input
5 63 4 1 2 8 66 1 8 2 7 45 9 3 9 9 58 4 1 3 2 63 7 2 8 6 45 63 4 1 2 8 66 1 8 2 7 45 9 3 9 9 58 4 1 3 2 63 7 2 0 9 10
Sample Output
1 2 3 4 4 5161 2 1 5 4 5111 119
Title: give you a n*m number table, find a left-to-right path, make the above number and minimum.
(each time can be from (I,J), walk to (i,j+1), (I+1,j), (I-1,J) can cross the border. When crossing the border, come out from above, come in from below, come out from above, come in from above, it is equivalent to the top of this figure and the bottom coincide to form a cylinder.
Analysis: DP, dynamic programming. Because the dictionary order is minimal, a right-to-left DP is used;
State: F (i,j) indicates the smallest and the i,j that go to (), there is a transfer equation:
F (i,j) = min (f (i+1,j+1), F (i,j+1), F (i-1,j+1));
Log the path output.
A two-dimensional array can be used when the path is recorded, path[j] [i] indicates that the elements of line J of column I are going to go to the path[J [i] row of the i + 1 column, so that the path can be saved.
Note: The inverse DP guarantees a minimum dictionary order (subsequent minimum), and the positive direction can guarantee the minimum of each point precursor.
Attached code:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < String> #define INF 0x3f3f3f3fusing namespace Std;int main () {int n,m;while (CIN >> n >> m) {Long long int dp[1 10][110];long long int map[110][110];for (int i = 1;i <= n;i++) {for (int j = 1;j <= m;j++) {scanf ("%lld", &map[i][j] );}} Memset (Dp,0,sizeof (DP)); DP Array, initialized to 0int path[110][110] = {0}; Saves an array of paths, initialized to 0for (int i = m;i >= 1;i--)//two-layer loop, the first layer represents the number of columns, and forwards the minimum value backwards. Similar to the number of towers {for (int j = 1;j <= n;j++) {Dp[j][i] = dp[j][i + 1] + map[j][i]; Take the positive post as the minimum value, and then make a comparison with the following path[j][i] = j; Record path if (J > 1 && dp[j-1][i + 1] + map[j][i] <= dp[j][i])//If J is greater than 1, then you can go to j-1 position and then judge J-1 position in line with the non-conformance, in accordance with the words of an update, because the j-1 is smaller than J, so the judgment here need to add "=", the following equals n is also the same time, the following is no longer {dp[j][i] = dp[j-1][i + 1] + map[j] [i];p ath[j][i] = j-1;} if (J < n && dp[j + 1][i + 1] + Map[j][i] < Dp[j][i])//if less than n, then you can go to the position of J + 1, the same judgment {dp[j][i] = dp[j + 1][i + 1] + map[j][i];p Ath[j] [I] = j + 1;} if (j = = 1 && dp[n][i + 1] + Map[j][i] < Dp[j][i])//Because 1 and n are more special, so, plus a judgment {dp[j][i] = dp[n][i + 1] + Map[j][i];p Ath[j][i] = n;} if (j = = n && dp[1][i + 1] + map[j][i] <= Dp[j][i]) {Dp[j][i] = dp[1][i + 1] + map[j][i];p ath[j][i] = 1;}} Long long int ans = dp[1][1];int Min_i = 1;for (int i = 1;i <= n;i++)//traverse over, look for the minimum value {if (ans > dp[i][1]) {min _i = I;ans = Dp[i][1];}} for (int i = 1;i <= m;i++)//based on the path of the tag, output the path {if (i > 1) cout << ""; cout << Min_i; Min_i = Path[min_i][i]; Assign the next location to Min_i}cout << endl;cout << ans << endl;} return 0;}
HDU1619 & UVA Unidirectional TSP (tree dp, Getting started, number of tower variants)