uva 10003 - Cutting Sticks( 表达式类型dp)

这道题目类似 矩阵乘法的 类型。

 

一根木快长 N   , 切一下 代价为 N  , 然后分别求 继续往下切的代价。。 题目中给出要切的点。

 

d[ i ][ j ] = j-i + min ( d[i][k] + d[k] [j] )       k属于(i,j) 

打一张表也是类似书上的倒三角样子

 

题目:

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.

 

 

 

代码:

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <memory.h>
 4 using namespace std;
 5 
 6 int arr[51];
 7 int d[1001][51];
 8 int n;
 9 int dp(int s,int e)
10 {
11     if(d[s][e]!=-1)
12     {
13         return d[s][e];
14     }
15 
16     int MIN=100000;
17 
18     for(int k = s+1;k<e;k++)
19         MIN =  min(MIN, dp(s,k)+dp(k,e));
20 
21     d[s][e] = arr[e]-arr[s] +MIN;
22 
23     return d[s][e];
24 }
25 void print()
26 {
27     cout<<n<<endl;
28     for(int i=0;i<=n;i++)
29     {
30         for(int j=0;j<=n;j++)
31         {
32             cout<<d[i][j] << " ";
33         }
34         cout<<endl;
35         cout<<n<<endl;
36     }
37     cout<<endl<<endl;
38 
39 }
40 int main()
41 {
42     int total;
43     while(cin>>total && total)
44     {
45         cin>>n;
46         arr[0]=0;
47         for(int i=1;i<=n;i++)
48         {
49             cin>>arr[i];
50         }
51         arr[n+1]=total;
52         sort(arr,arr+n);
53         n+=1;
54         memset(d,-1,sizeof(d));
55 
56         for(int i=0;i<=n;i++)
57         {
58             d[i][i]=0;
59             d[i][i+1]=0;
60         }
61 
62         for(int i =0;i<=n;i++)
63         {
64             for(int step=2; step<=n;step++)
65             {
66                 if(i+step<=n)
67                 {
68                     int MIN = 100000;
69                     for(int kk = i+1; kk< i+step;kk++)
70                         MIN =  min(MIN, dp ( i,kk ) + dp(kk , i+step) );
71 
72                      d[i][i+step] = arr[i+step] -arr[i] + MIN;
73 
74 
75                      //print();
76                 }
77             }
78         }
79 
80         cout<<"The minimum cutting is "<<d[0][n]<<"."<<endl;
81 
82     }
83 
84 
85 
86     return 0;
87 }

 

posted @ 2013-12-29 18:44  doubleshik  阅读(252)  评论(0编辑  收藏  举报