uva00116,

# Unidirectional TSP

## 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 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.

## The Problem

Given an  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 columni 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  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.

## The 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.

## The 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.

```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```

Solution:
Using dynamic programming: consume[i][j] = min{consume[i][j+1], consume[i-1][j+1], consume[i+1][j+1]} + matrix[i][j];
PS: Computer from right to left will result lexicographically.
Backtracking might be the optional way.
Test cases:
```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
12 14
1 2 2 1 1 1 1 1 1 1 1 1 1 2
1 1 2 2 1 1 1 1 1 1 1 1 1 1
1 1 1 2 2 1 1 1 1 1 1 1 1 1
1 1 1 1 2 2 1 1 1 1 1 1 1 1
1 1 1 1 1 2 2 1 1 1 1 1 1 1
1 1 1 1 1 1 2 2 1 1 1 1 1 1
1 1 1 1 1 1 1 2 2 1 1 1 1 1
1 1 1 1 1 1 1 1 2 2 1 1 1 1
1 1 1 1 1 1 1 1 1 2 2 1 1 1
1 1 1 1 1 1 1 1 1 1 2 2 1 1
1 1 1 1 1 1 1 1 1 1 1 2 1 1
1 1 1 1 1 1 1 1 1 1 1 1 2 2
4 7
1 2 -3 4 -2 1 5
-1 3 5 -2 6 -3 4
2 1 3 -2 -1 3 1
3 -3 4 2 -3 4 3
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
5 6
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
3 4
1 2 3 4
1 2 3 4
1 2 3 4
5 5
1 5 10 6 3
5 1 8 4 11
10 12 5 2 9
7 3 20 5 8
4 1 5 12 6
5 10
11 53 34 73 18 53 99 52 31 54
4 72 24 6 46 17 63 82 89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#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define INT_MAX 2147483647
#define INT_MIN -2147483647

using namespace std;

class Point {
public:
int x;
int y;
};

void init(int m, int n, vector<vector<int> > &matrix,
vector<vector<Point> > &parent) {
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
parent[i][j].x = i;
parent[i][j].y = j;
}
//  for (int i = 0; i < m; i++) {
//      for (int j = 0; j < n; j++)
//          cout << matrix[i][j] << " ";
//      cout << endl;
//  }
}

void reverseVector(vector<vector<int> > &v) {
for (int i = 0; i < (int) v.size(); i++)
reverse(v[i].begin(), v[i].end());
}

bool cmp(const vector<int> A, const vector<int> B) {
for (int i = 0; i < (int) A.size(); i++) {
if (A[i] > B[i])
return false;
if (A[i] < B[i])
return true;
}
return true;
}

pair<int, vector<int> > Path(int m, int n, vector<vector<int> > &matrix,
vector<vector<Point> > &parent) {
vector<vector<int> > consume(m, vector<int> (n, INT_MAX));
//  for (int i = 0; i < m; i++) {
//      for (int j = 0; j < n; j++)
//          cout << consume[i][j] << " ";
//      cout << endl;
//  }
for (int i = 0; i < m; i++)
consume[i][n - 1] = matrix[i][n - 1];
//consume[0][0] = matrix[0][0];

for (int j = n - 2; j >= 0; j--) {
for (int i = 0; i < m; i++) {
vector<int> pos;
pos.push_back(i);
pos.push_back((i + 1) % m);
pos.push_back((i + m - 1) % m);
sort(pos.begin(), pos.end());
for (int k = 0; k < 3; k++) {
if (consume[pos[k]][j + 1] != INT_MAX && consume[i][j]
> matrix[i][j] + consume[pos[k]][j + 1]) {
consume[i][j] = matrix[i][j] + consume[pos[k]][j + 1];
parent[i][j].x = pos[k];
parent[i][j].y = j + 1;
}
}
}
}
//  for (int j = 0; j < n; j++)
//      for (int i = 0; i < m; i++) {
//          cout << consume[i][j] << ": " << parent[i][j].x << ", "
//                  << parent[i][j].y << endl;
//      }
//  for (int i = 0; i < m; i++) {
//      for (int j = 0; j < n; j++)
//          cout << consume[i][j] << " ";
//      cout << endl;
//  }
int mmin = INT_MAX;
for (int i = 0; i < m; i++) {
if (mmin > consume[i][0]) {
mmin = consume[i][0];
}
}
vector<vector<int> > ret;
for (int i = 0; i < m; i++) {
if (consume[i][0] == mmin)
ret.push_back(vector<int> (1, i));
}
for (int j = 0; j < (int) ret.size(); j++) {
int xx = ret[j][0];
int yy = 0;
ret[j][0]++;
for (int i = 0; i < n - 1; i++) {
xx = parent[xx][yy].x;
yy = parent[xx][yy].y;
ret[j].push_back(xx + 1);
}
}
//  reverseVector(ret);
//  for (int i = 0; i < ret.size(); i++) {
//      for (int j = 0; j < ret[i].size(); j++)
//          cout << ret[i][j] << " ";
//      cout << endl;
//  }
sort(ret.begin(), ret.end(), cmp);
//  for (int i = 0; i < ret.size(); i++) {
//      for (int j = 0; j < ret[i].size(); j++)
//          cout << ret[i][j] << " ";
//      cout << endl;
//  }
return make_pair(mmin, ret[0]);
}

void print(pair<int, vector<int> > v) {
cout << v.second[0];
for (int i = 1; i < (int) v.second.size(); i++)
cout << " " << v.second[i];
cout << endl;
cout << v.first << endl;
}

int main(int argc, char* argv[]) {
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
int m, n;
while (cin >> m >> n) {
vector<vector<int> > matrix(m, vector<int> (n, 0));
vector<vector<Point> > parent(m, vector<Point> (n));
init(m, n, matrix, parent);
print(Path(m, n, matrix, parent));
}
//fclose(stdin);
//fclose(stdout);
return 0;
}