uva10003,

# Cutting Sticks

You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they only make one cut at a time.It is easy to notice that different selections in the order of cutting can led to different prices. For example, consider a stick of length 10 meters that has to be cut at 2, 4 and 7 meters from one end. There are several choices. One can be cutting first at 2, then at 4, then at 7. This leads to a price of 10 + 8 + 6 = 24 because the first stick was of 10 meters, the resulting of 8 and the last one of 6. Another choice could be cutting at 4, then at 2, then at 7. This would lead to a price of 10 + 4 + 6 = 20, which is a better price.
Your boss trusts your computer abilities to find out the minimum cost for cutting a given stick.

## Input

The input will consist of several input cases. The first line of each test case will contain a positive number l that represents the length of the stick to be cut. You can assume l < 1000. The next line will contain the number n (n < 50) of cuts to be made.The next line consists of n positive numbers ci ( 0 < ci < l) representing the places where the cuts have to be done, given in strictly increasing order.
An input case with l = 0 will represent the end of the input.

## Output

You have to print the cost of the optimal solution of the cutting problem, that is the minimum cost of cutting the given stick. Format the output as shown below.

## Sample Input

100
3
25 50 75
10
4
4 5 7 8
0


## Sample Output

The minimum cutting is 200.
The minimum cutting is 22.


Miguel Revilla
2000-08-21

Solution:
Using dynamic programming.

The answer is basically try everything and take the minimum - make a cut everywhere, and try to answer that subproblem that comes up.
So the function $C( a_1, a_2, \ldots, a_n )$ where ai are places where cuts are possible. In particular, a1 and an will serve as the end points, since it never makes sense to make a cut at an edge (and thus having the same problem).
When we make a cut at ai, it gives us two subproblems - the left side, and the right side. With the base case C(ai,ai + 1) = 0 (which basically means if there is one board with no more cuts to be made, the cost is zero), we can now formulate the recurrence: $C(a_i, a_j) = \min_{k \in (i+1, \ldots, j-1)} \left[ C(a_i, a_k) + C(a_k, a_j) \right]+cost(a_i,a_j)$
The cost of making a cut is simply:
cost(ai,aj) = aj - ai
PS: using C-format arrays will save a lot of time, time consume reduced from TLE to 0.46

## Source Code:

//Sun Apr 10 03:06:20 CDT 2011
#include <vector>
#include <list>
#include <map>
#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 = matrix;

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]) {
mmin = consume[i];
}
}
vector<vector<int> > ret;
for (int i = 0; i < m; i++) {
if (consume[i] == mmin)
ret.push_back(vector<int> (1, i));
}
for (int j = 0; j < (int) ret.size(); j++) {
int xx = ret[j];
int yy = 0;
ret[j]++;
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);
}

void print(pair<int, vector<int> > v) {
cout << v.second;
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;
}