22809_TheSalesmanStillTravels

2022-5-16 18:23| 发布者: Hocassian| 查看: 29| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-5-89506977572800-Problem List-采集的数据-后羿采集器.html

Pro.ID

22809

Title

The Salesman Still Travels

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=22809

AC

4

Submit

7

Ratio

57.14%

时间&空间限制

  • Time Limit: 1000/500 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    The Travelling Salesman Problem (TSP) is a legendary problem in computer science, an example of an NP-complete problem. Here, you will deal with a much simplified version.

    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 sequence of steps terminating in column n ( the last column ). A step consists of travelling 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 in Figure 3.

    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 in Figure 4 ( 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.

    输入

    The input consists of a series of matrix specifications. Each matrix specification consists of the row and column dimensions in that order on a line followed by m*n 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 the input.

    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 30 bit integer values.

    输出

    Description

    The Travelling Salesman Problem (TSP) is a legendary problem in computer science, an example of an NP-complete problem. Here, you will deal with a much simplified version.

    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 sequence of steps terminating in column n ( the last column ). A step consists of travelling 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 in Figure 3.

    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 in Figure 4 ( 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 series of matrix specifications. Each matrix specification consists of the row and column dimensions in that order on a line followed by m*n 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 the input.

    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 30 bit integer values.

    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 space ) 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

    样例输入

    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

    样例输出

    1 2 3 4 4 5
    16
    1 2 1 5 4 5
    11
    1 1
    19

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部