HDOJ 题目1619 Unidirectional TSP(DP输出路径啊)

Unidirectional TSP

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 427    Accepted Submission(s): 212


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
 

Source
UVA
 

Recommend
8600   |   We have carefully selected several similar problems for you:   1591  1702  1683  3337  1111 
 
题目大意:每列一个数,从左走到右的走过的最小的和,可以有三个方向走如图上,注意当在第1行可以向上走到底n行,第n行可以向下走到第1行,输出路径,如果有多个路径输出最小的字典序的,
周赛题,HDU的数据比较水,我的姿势HDU可过,UVA一直过不了= =,先留着代码。。。
正资:http://www.myexception.cn/program/1393700.html
ac代码
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#define INF 0x3f3f3f3f3f3f3f
#define LL long long
using namespace std;
#define min(a,b) (a<b?a:b)
LL map[1010][1010];
struct s
{
    int x,y;
}pre[1010][1010];
LL dp[1010][1010];
int ans[1010];
int n,m;
void DP()
{
    int i,j;
    /*dp[1][1]=map[1][1];
    dp[1][2]=map[1][2]+dp[1][1];
    pre[1][2].x=1;
    pre[1][2].y=1;
    dp[2][2]=map[2][2]+dp[1][1];
    pre[2][2].x=1;
    pre[2][2].y=1;
    dp[n][2]=map[n][2]+dp[1][1];
    pre[n][2].x=1;
    pre[n][2].y=1;*/
    for(i=1;i<=n;i++)
        dp[i][1]=map[i][1];
    for(j=2;j<=m;j++)
    {
        for(i=1;i<=n;i++)
        {
            LL aa,bb,cc;
            if(i==1)
            {
                aa=dp[n][j-1];
            }
            else
                aa=dp[i-1][j-1];
            bb=dp[i][j-1];
            if(i==n)
                cc=dp[1][j-1];
            else
                cc=dp[i+1][j-1];
            LL temp;
            temp=min(aa,min(bb,cc));
            dp[i][j]=temp+map[i][j];
            int pa,pb,pc;
            pa=pb=pc=INF;
            if(temp==aa)
            {
                if(i==1)
                {
                    pa=n;
                }
                else
                    pa=i-1;
            }
            if(temp==cc)
            {
                if(i==n)
                    pc=1;
                else
                {
                    pc=i+1;
                }
            }
            if(temp==bb)
            {
                pb=i;
            }
            pre[i][j].x=min(pa,min(pb,pc));
            pre[i][j].y=j-1;
        }
    }
}
void GET()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            scanf("%lld",&map[i][j]);
            dp[i][j]=INF;
            pre[i][j].x=-1;
            pre[i][j].y=-1;
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        GET();
        DP();
        int flag=0;
        LL minn=INF;
        int i,j;
        for(i=1;i<=n;i++)
        {
            if(dp[i][m]<minn)
            {
                minn=dp[i][m];
                flag=i;
            }
        }
        int x=flag,y=m;
        memset(ans,0,sizeof(ans));
        while(1)
        {
            int tx,ty;
            ans[y]=x;
            if(y==1)
                break;
            tx=pre[x][y].x;
            ty=pre[x][y].y;
            x=tx;
            y=ty;
        }
        printf("%d",ans[1]);
        for(i=2;i<=m;i++)
        {
            printf(" %d",ans[i]);
        }
        printf("\n");
        printf("%lld\n",minn);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值