Unidirectional TSP

Unidirectional TSP

时间: 1ms        内存:64M

描述:

Given an m x n matrix of integers, you are to write a program that computes a path of minimal weight from left to right across the matrix. A path starts anywhere in column 1 and consists of a sequence of steps terminating in column n. Each 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. The minimum paths through two slightly different 5 x 6 matrices are shown below. The matrix values differ only in the bottom row. The path for the matrix on the right takes advantage of the adjacency between the first and last rows.

输入:

The input consists of a sequence of matrix specifications. Each matrix consists of the row and column dimensions on a line, denoted m and n, respectively. This is followed by m . n integers, appearing 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.

输出:

Two lines should be output for each matrix specification. The first line represents a minimal-weight path, and the second line is the cost of this 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 lexicographically smallest path should be output.

示例输入:

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

提示:

参考答案(内存最优[1280]):

#include<iostream>
using namespace std;

int g[15][105];
int dp[15][2];
int path[15][105];

void update(int row, int col, int LastRow)
{
     if(dp[row][col%2] == -1)
     {
         dp[row][col%2] = dp[LastRow][(col+1)%2] + g[row][col];
         path[row][col] = LastRow;
     } 
     else if(dp[row][col%2] == (dp[LastRow][(col+1)%2] + g[row][col]))
     {
          if(path[row][col] > LastRow)
             path[row][col] = LastRow;
     }
     else if(dp[row][col%2] > (dp[LastRow][(col+1)%2] + g[row][col]))
     {
         dp[row][col%2] = dp[LastRow][(col+1)%2] + g[row][col];
         path[row][col] = LastRow;
     }
}

int main()
{
    int row, col;
    while(cin>>row)
    {
             cin>>col;
             for(int i = 0; i < row; ++i)
                     for(int j = 0; j < col; ++j)
                     {
                             cin>>g[i][j];
                     }      
             
             for(int i = 0; i < row; ++i)
             {
                 dp[i][(col+1)%2] = g[i][col-1];
             }
                 
             for(int j = col-2; j >= 0; j--)
             {
                     for(int i = 0; i < row; ++i)
                     {
                             dp[i][j%2] = -1;
                     }
                     
                     for(int i = 0; i < row; ++i)
                     {
                             int lastRow = i - 1;
                             if(lastRow < 0) lastRow = row-1;
                             update(i, j, lastRow);
                             update(i, j, i);
                             lastRow = i + 1;
                             if(lastRow > row-1) lastRow = 0;
                             update(i, j, lastRow);
                     }
                    /* 
                     for(int i = 0; i < row; ++i)
                     {
                             cout<<dp[i][j%2]<< " ";
                     }
                     cout<<endl;*/
             }
             
             int index = 0, minValue = dp[0][0];
             for(int i = 1; i < row; ++i)
             {
                     if(minValue > dp[i][0])
                     {
                         minValue = dp[i][0];
                         index = i;
                     }
             }
             cout<<index + 1;
             for(int i = 0; i < col-1; ++i)
             {
                     cout<<" "<<path[index][i] + 1;
                     index = path[index][i];
             }
             cout<<endl<<minValue<<endl;
    }
    
    return 0;
}

参考答案(时间最优[0]):

#include<iostream>
using namespace std;

int g[15][105];
int dp[15][2];
int path[15][105];

void update(int row, int col, int LastRow)
{
     if(dp[row][col%2] == -1)
     {
         dp[row][col%2] = dp[LastRow][(col+1)%2] + g[row][col];
         path[row][col] = LastRow;
     } 
     else if(dp[row][col%2] == (dp[LastRow][(col+1)%2] + g[row][col]))
     {
          if(path[row][col] > LastRow)
             path[row][col] = LastRow;
     }
     else if(dp[row][col%2] > (dp[LastRow][(col+1)%2] + g[row][col]))
     {
         dp[row][col%2] = dp[LastRow][(col+1)%2] + g[row][col];
         path[row][col] = LastRow;
     }
}

int main()
{
    int row, col;
    while(cin>>row)
    {
             cin>>col;
             for(int i = 0; i < row; ++i)
                     for(int j = 0; j < col; ++j)
                     {
                             cin>>g[i][j];
                     }      
             
             for(int i = 0; i < row; ++i)
             {
                 dp[i][(col+1)%2] = g[i][col-1];
             }
                 
             for(int j = col-2; j >= 0; j--)
             {
                     for(int i = 0; i < row; ++i)
                     {
                             dp[i][j%2] = -1;
                     }
                     
                     for(int i = 0; i < row; ++i)
                     {
                             int lastRow = i - 1;
                             if(lastRow < 0) lastRow = row-1;
                             update(i, j, lastRow);
                             update(i, j, i);
                             lastRow = i + 1;
                             if(lastRow > row-1) lastRow = 0;
                             update(i, j, lastRow);
                     }
                    /* 
                     for(int i = 0; i < row; ++i)
                     {
                             cout<<dp[i][j%2]<< " ";
                     }
                     cout<<endl;*/
             }
             
             int index = 0, minValue = dp[0][0];
             for(int i = 1; i < row; ++i)
             {
                     if(minValue > dp[i][0])
                     {
                         minValue = dp[i][0];
                         index = i;
                     }
             }
             cout<<index + 1;
             for(int i = 0; i < col-1; ++i)
             {
                     cout<<" "<<path[index][i] + 1;
                     index = path[index][i];
             }
             cout<<endl<<minValue<<endl;
    }
    
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注