Unidirectional TSP (DP)

Source: Internet
Author: User

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 columnI 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 integers 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 row of the M Atrix, 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
Main topic:

give you a matrix of n * m, each element of the matrix has a corresponding value, find a path, from any point in a column to go to the bottom of the M-column any point out, you can go to the three direction, when the first row, go to the upper right corner of the line can be the most straight to the nth row ( The same as when the nth line goes down to the right). Go to the end to find the lowest value of a path each time the row, requires the output dictionary order the smallest path.

Problem Solving Ideas:

The idea of using a digital triangle can go from one point to the next, adding the previous column to the minimum value of the point, and then to the last row. This method to find the path of the smallest dictionary order when the trouble, I did not write it out, so a change of thought, from the last column has been added to the first column, each access to the minimum dictionary sequence scheme, and then find the solution.

The code is as follows:

#include <iostream> #include <cstdio> #include <map> #include <math.h> #include <cstring>    #include <algorithm>using namespace Std;int main () {int i, j,vis[20][110], M, N;    int dp[20][110], map[20][110];        while (scanf ("%d%d", &m,&n)! = EOF) {memset (map,0,sizeof (MAP));        Memset (Dp,0,sizeof (DP)); for (i = 1; I <= m; i++) {for (j = 1; J <= N; j + +) {scanf ("%d", &map[            I][J]); }} for (i = n; I >= 1; i--) {for (j = 1; j <= M; j + +) {D    P[j][i] = Map[j][i] + dp[j][i + 1];//plus go forward vis[j][i] = j; The VIS array is saved each time the path if (J > 1 && (dp[j][i] >= Map[j][i] + dp[j-1][i + 1]))///If the King right-hand walk is less than or equal to the current value to be updated because                       Small dictionary order {Dp[j][i] = Map[j][i] + dp[j-1][i + 1];                Vis[j][i] = j-1; } if (j = = M && (Dp[j][i] >= map[j][i] + dp[1][i +1]))                The same dictionary order is minimal to update.                       {Dp[j][i] = Map[j][i] + dp[1][i + 1];                Vis[j][i] = 1; } if (J < m && (Dp[j][i] > Map[j][i] + dp[j + 1][i + 1])//You can only judge this one first, because if you first judge the following j = = 1, the dictionary order has a feeling                       The condition is not optimal {dp[j][i] = Map[j][i] + dp[j + 1][i +1];                Vis[j][i] = j + 1; } if (j = = 1 && (Dp[j][i] > Map[j][i] +dp[m][i + 1])) {Dp[j                       ][i] = Map[j][i] + dp[m][i + 1];                Vis[j][i] = m;        }}} int besti;        int minx;                Besti = 1;//Find the optimal scheme for (i = 2; I <= m; i++) {if (Dp[i][1] < dp[besti][1]) {             Besti = i;        }} printf ("%d", besti);        Minx = dp[besti][1]; Look down and then come out for (i = 1; i < N            i++) {printf ("%d", vis[besti][i]);        Besti = Vis[besti][i];        } printf ("\ n");    printf ("%d\n", Minx); } return 0;}




Unidirectional TSP (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.