Unidirectional TSP
Time limit:3000ms Memory limit:0kb 64bit IO format:%lld &%llu
Submit Status Practice UVA 116
Appoint Description:
Description
Download as PDF
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 on a salesperson ' s route can be visited exactly o NCE with a specified limit on 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 tex2html_wrap_inline352 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 Las T 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.
Picture25
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, slightly different tex2html_wrap_inline366 matrices was shown below (the only difference was the numbers I n the bottom row).
Picture37
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 Tex2html_wrap_inli ne376 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 matrix, th e 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 constitute 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 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
/*AUTHOR:ZCC; solve:dp[I][J]: Indicates to the (I,J) point the minimum value similar to the tower state equation is dp[I][J]=min{dp[I][j-1],dp[i+1][j-1],dp[i-1][j-1]}+a[I][J]; The key is the record output of the path, here I'm using the pair<int,int>*/#include <iostream>#include <algorithm>#include <map>#include <cstdio>#include <cstdlib>#include <vector>#include <cmath>#include <cstring>#include <stack>#include <string>#include <fstream>#define PB (s) push_back (s)#define CL (A, B) memset (A,b,sizeof (a) )#define BUG printf ("===\n");using namespace Std;typedef vector<int>VI;#define REP (A, b) for (int i=a;i<b;i++)#define REP_ (A, b) for (int i=a;i<=b;i++)#define P pair<int,int>#define BUG printf ("===\n");#define PL (x) push_back (x)#define X First#define Y Second#define VI vector<int>#define REP (i,x,n) for (int i=x;i<n;i++)#define REP_ (i,x,n) for (int i=x;i<=n;i++)const int Maxn=1100;const int inf=999999;typedef long long LL; LL a[ $][MAXN]; LL dp[ $][MAXN]; P p[ $][MAXN];//Record path int main () {//Freopen ("Text/in.txt", "R", stdin);//freopen ("Text/myout.txt", "w", stdout);int n,m;while (~scanf ("%d%d", &n,&m)) {for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {scanf ("%lld", &a[i][j]);P[i][j]. X=P[I][J]. y=0; } }cl (dp,0);for (int j=m;j>=1;j--) {//In order to guarantee the smallest dictionary order, here is a reverse enumeration. for (int i=1;i<=n;i++) {int x=i+1;int y=i-1;if (x==n+1) x=1;if (y==0) y=n;//dp[i][j]=min (Dp[i][j-1],min (dp[x][j-1],dp[y][j-1)) +a[i][j];LL dp1=dp[i][j+1];LL dp2=dp[x][j+1];LL dp3=dp[y][j+1];if (dp1<=dp2&&dp1<=dp3) {Dp[i][j]=dp1+a[i][j];if (P[i][j]. x==0| | P[I][J]. X>i) p[i][j]=p (i,j+1); }if (dp2<=dp1&&dp2<=dp3) {Dp[i][j]=dp2+a[i][j];if (P[i][j]. x==0| | P[I][J]. x>x) p[i][j]=p (x,j+1); }if (DP3<=DP1&&DP3<=DP2) {Dp[i][j]=dp3+a[i][j];if (!p[i][j]. x| | P[I][J]. X>y) p[i][j]=p (y,j+1); } } }LL Mi=inf; P Pos;for (int i=1;i<=n;i++) if (mi>dp[i][1]) {mi=dp[i][1];p os. x=i;Pos. Y=1; }for (int i=1;i<m;i++) {printf ("%d", Pos. X);Pos=p[pos. X][pos. Y]; }printf ("%d\n%lld\n", Pos. X,MI); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA unidirectional tsp one-way TSP problem, classic DP (Path output note planning direction)