10003-cutting Sticks
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_ problem&problem=944
You are 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 a/a time.
It is easy to notice so different selections in the order of cutting can LEDs to different prices. For example, consider a stick the length meters that has to is cut at 2, 4 and 7 meters from one end. There are several choices. One can be cutting in 2, then at 4, then at 7. This is leads to a price of + 8 + 6 = Because the stick is meters, the resulting of 8 and the last one of 6. Another choice could is cutting at 4, then at 2, then at 7. This would leads to a price of + 4 + 6 = which are a better price.
Your boss trusts Your computer abilities the minimum cost for cutting a given stick.
Input
The input would consist the several input cases. The the ' the ' of each test case would contain a positive number l that represents the length of the ' stick to being cut. You can assume L < 1000. The next line would contain the number n (n < m) 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 being done and given in strictly increasing order.
An input case with l = 0 would represent the end of the input.
Output
You are have to print the cost of the optimal solution of the cutting problem, which is the minimum cost of cutting the given S Tick. Format the output as shown below.
Sample Input
3
4
4 5 7
8 0
Sample Output
The minimum cutting is.
The minimum cutting is 22.
Under the "charges money according to the length of the stick being cut",
We have: dp[i][j] = Min{dp[i][k] + dp[k][j]} + c[j]-c[i]
Here C[i] and C[j] are the starting and ending points for the sticks.
Complete code:
/*0.089s*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace Std;
const int INF = 0X3F3F3F3F;
int c[60], dp[60][60];
int main (void)
{
int len, n;
while (scanf ("%d", &len), Len)
{
scanf ("%d", &n);
C[0] = 0, c[n + 1] = len;
for (int i = 1; I <= n; ++i)
scanf ("%d", &c[i));
memset (DP, 0, sizeof (DP));
for (int p = 2; p <= n + 1; ++p)///starts with a 2-paragraph stick to merge for
(int i = 0; I <= n + 1-p; ++i)///i is the starting point, J is the endpoint
{
int j = i + p, Min = INF;
for (int k = i + 1; k < J; ++k)
min = min (min, dp[i][k] + dp[k][j]);
if (Min!=inf)
dp[i][j] = Min + c[j]-c[i];
}
printf ("The minimum cutting is%d.\n", Dp[0][n + 1]);
}
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/