您的位置:首页 > 其它

HDU 1619:Unidirectional TSP【dfs & 回忆路径】

2015-12-27 22:08 483 查看


Unidirectional TSP

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 429    Accepted Submission(s): 214


Problem Description

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 path through a grid of points while traveling only from left to right. 

Given an m*n matrix of integers, you are 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 are considered adjacent, i.e., the matrix ``wraps'' so that it represents a horizontal cylinder. Legal steps are illustrated below. 



The weight of a path is the sum of the integers in each of the n cells of the matrix that are visited. 

For example, two slightly different 5*6 matrices are shown below (the only difference is the numbers in the bottom row). 



The minimal path is illustrated for each matrix. Note that the path for the matrix on the right takes advantage of the adjacency property of the first and last rows. 

 

Input

The input consists of a sequence of matrix specifications. Each matrix specification consists of the row and column dimensions in that 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 matrix, the second n 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 will be between 1 and 10 inclusive; the number of columns will be between 1 and 100 inclusive. No path's weight will exceed integer values representable using 30 bits

 

Output

Two lines should be output for each matrix specification in the input file, the first line represents a minimal-weight path, and the second line is 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 is 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

题意:要求从左边到右边(注意第1行和最后一行相邻),即:从第1列到最后一列即可,共有三种走法,求最小的代价及路径(只有行数)。

分析:按顺序输出路径时,只有三个方向,从小到大排序一下,再搜索路径。

 AC-code:

#include<cstdio>
#include<algorithm>
#include<cstring>
#define max 0x3f3f3f
#define min(a,b) a>b?b:a
int dx[3][2]={-1,1,0,1,1,1};
using namespace std;
int dp[15][105],k,path[105],m,n,flag,c[3],a[15][105];
int dfs(int x,int y)
{
int x1,y1,i;
if(dp[x][y]!=max)//已遍历
return dp[x][y];
if(y==m)//最后一列
return dp[x][y]=a[x][y];
for(i=0;i<3;i++)
{
x1=x+dx[i][0];
y1=y+dx[i][1];
if(!x1)
x1=n;
if(x1>n)
x1=1;
dp[x][y]=min(dp[x][y],dfs(x1,y1));//每一个点的后面的最短路
}
return dp[x][y]=dp[x][y]+a[x][y];//把后面的路长和加到前一个上
}
void findp(int x,int y)
{
int x1,y1,i;
if(flag)//已结束
return;
path[k++]=x;//记载路径
if(y==m)
{
flag=1;
return ;
}
for(i=0;i<3;i++)
{
x1=x+dx[i][0];
if(!x1)
x1=n;
if(x1>n)
x1=1;
c[i]=x1;
}
sort(c,c+3);//排序。。从小到大。。。
for(i=0;i<3;i++)
{
y1=y+dx[i][1];
if(dp[x][y]==dp[c[i]][y1]+a[x][y])//确定路径
findp(c[i],y1);//往下找
}
}
int main()
{
int mmin,i,u,j;
while(~scanf("%d%d",&n,&m))
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
dp[i][j]=max;
}
mmin=max;
for(i=1;i<=n;i++)
{
if(mmin>dfs(i,1))//搜索最小路
{
mmin=dp[i][1];
u=i;
}
}
k=flag=0;
memset(path,0,sizeof(path));
findp(u,1);//找路径
for(i=0;i<k-1;i++)
printf("%d ",path[i]);
printf("%d\n",path[k-1]);
printf("%d\n",mmin);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: