Title:
G- unidirectional TSP
Time Limit: 3000MS Memory Limit: 0KB 64bit IO Format: %lld &%llu
Submit Status Practice UVA
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 is 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 on travel time - - is one of the canonical examples of an np-complete problem ; solutions appear to require an inordinate amount of time To generate, but are 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 anmatrix 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 columnN(the last column). A step consists of traveling from columnITo columnI+1 in an adjacent (horizontal or diagonal) row. The first and last rows (rows 1 andm) of a matrix is considered adjacent, i.e., the matrix ' wraps ' so that 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 that is visited.
For example, 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 byintegers wheremIs the row dimension andNIs the column dimension. The integers appear in the input in row major order, i.e., the firstNintegers constitute the first row of the Matrix, the secondN integers constitute the second row and so on. the integers on a line will be separated from Other integers by one or more spaces. note: integers are not restricted to being positive. there will be 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 lines should is output for each of the matrix specification in the input file Path, and the second line are the cost of a minimal path. The path consists of a sequence ofNintegers (separated by one or more spaces) representing the rows that constitute the minimal path. If there is more than one path of minimal weight the pathlexicographicallysmallest should be output.
Sample 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
Sample Output
1 2 3 4 4 5
16
1 2 1 5 4 5
11
1 1
19
Main topic:to a figure, the smallest value from left to right, only to the top right, lower right, the right side of these three directions. Minimum value. If multiple values are used, the dictionary order is minimized. Notice that the top and bottom are attached.Topic Ideas:1, because the subsequent selection does not change the previous state, you can use the DP2, because the dictionary order, we can reverse the request, that is, from right to left.3, we can open a group more, to determine the source location, convenient to seek answers.4, note the case width of 1Program:
#include <cstdio> #include <algorithm> #include <iostream> #include <cstring>using namespace Std;int A[110][110];int v[110][110];int n,m;void Maxx (int x,int y) {if (x==0)//topmost {int w=x,da=a[x][y+1]; if (A[x+1<n?x+1:0][y+1]<da) {w=x+1<n?x+1:0; da=a[x+1<n?x+1:0][y+1]; } if (A[n-1][y+1]<da) {w=n-1; DA=A[N-1][Y+1]; } A[x][y]+=da; V[x][y]=w; } else if (x==n-1)//Bottom {int w=0,da=a[0][y+1]; if (A[x-1][y+1]<da) {w=x-1; DA=A[X-1][Y+1]; } if (A[x][y+1]<da) {w=x; DA=A[X][Y+1]; } A[x][y]+=da; V[x][y]=w; } else//other {int w=x-1,da=a[x-1][y+1]; if (A[x][y+1]<da) {w=x; DA=A[X][Y+1]; } if (A[x+1][y+1]<da) {w=x+1; DA=A[X+1][Y+1]; } A[x][y]+=da; v[x][y]=w;//Mark position}}int main () {int i,j,k,da,w; int t[110]; while (~SCANF ("%d%d", &n,&m)) {for (i=0; i<n; i++) for (j=0; j<m; j + +) SCA NF ("%d", &a[i][j]);//input for (j=m-2; j>=0; j--)//right-to-left for (i=0; i<n; i++) Maxx (I,J) ;//Seek answers da=a[0][0],w=0; for (I=1; i<n; i++) if (a[i][0]<da) {da=a[i][0];//search for answers w=i; } printf ("%d", w+1); for (j=0; j<m-1; J + +) {W=v[w][j]; printf ("%d", w+1);//Output answer} printf ("\n%d\n", DA); } return 0;}
UVA unidirectional TSP