An introduction to the problem of steel bar cutting in reading notes
Qiao is clumsy (welcome reprint, but please specify Source: Http://blog.csdn.net/qiaoruozhuo )
given a length of N inch of steel rods and a price list Pi (i=1,2, ..., n) , to cut the steel bar scheme, so that the sales proceeds RN Maximum. Note that if the length of the steel bar with an n -inch is large enough, the optimal solution may be that it does not need to be cut at all.
if the length of the steel rods is I , the price of the steel rods is Pi , how to cut a given length of steel to get the most benefit?
length I 1 2 3 4 5 6 7 8 9
Price Pi 1 5 8 9
i = 1 , the steel rods are not cut, r[1]= 1 ;
i = 2 , the steel rods can be divided into 1+ 1 , whose price is 2 . If not split (0 + 2), the price is 5. i.e. r[2] = 5;
i = 3 , the steel rods can be divided into 0+ 3 , 1 + 2 . R[3] = 8;
In the same vein, you get:
r[4] = ten ( 2 );
R[5] = ( 3 );
R[6] = ( 0+ 6 );
R[7] = ( 1+ 6 or 4+ 3=> 2 + 2 + 3 );
.......
We can see that the length is 7 4 3 4 The optimal solution is r[4] = ten ( 2 + 2 3 The optimal solution is r[3] = 8 r[7] =r[4]+ r[3] =>
We will cut the steel bar to the left of the length of i n-i n The steel bar is broken down to the beginning of the left, And the remainder continues to decompose. Thus, a scheme that does not do any cutting can be described as: the length of the first paragraph is n Span style= "COLOR: #333333" > pn 0 r0=0
therefore, in the calculation R[i] , the value that is evaluated is r[0] +r[i] , r[1]+ R[i-1] , r[2]+ R[i-2] , ... , R[i-1] +r[1] while in dynamic planning, the maximum value of the R[0]--r[i-1] the value in the calculation R[i] It has been saved before, and the best results can be achieved with a small number of operations.
There are two equivalent methods for solving the problem of optimal steel bar cutting using dynamic programming algorithm.
The first method is called a top-down method with a memo. This method writes the procedure recursively, but the process saves the solution of each sub-problem--using an array of r[i] to record the maximum return value of the steel pipe with a total length of I. When a solution is required for a sub-problem, the procedure first checks to see if the solution has been saved, and if so, returns the saved value directly or recursively computes the sub-problem.
The second method is called the bottom-up method, which is a common method of dynamic programming, which requires us to sort the sub-problems according to their size and solve them in order from small to large-in the case of I from 1 to N in order to solve. Each sub-problem is solved only once, and the results are recorded in time for later invocation.
The algorithm obtained by the two methods has the same progressive run time, the only difference is that in some special cases, the top-down method does not really recursively investigate all possible sub-problems. Since there is no overhead of frequent recursive function calls, the time complexity function of the bottom-up method usually has a smaller coefficient.
The code is as follows:
/*name: Steel bar cutting problem Copyright:author: Qiao if clumsy date:12-12-14 19:49description: */#include <stdio.h> #include <stdlib.h > #define MAX 20001//MAX length #define INFINITY-999999//negative infinity void printcutrodsolution (int p[], int n);//Bottom-up method void Bott Omupcutrod (int p[], int r[], int s[], int n);//The maximum return value of a steel tube with a total length of I r[i] and the cut length of the first segment of the bar S[i] void Memoizedcutrod (int p[], int n);// Top-down method with memo int memoizedcutrodaux (int p[], int r[], int s[], int n);//recursive calculation R[i] value int main () {int P[max] = {0};int I, n = 50;f or (I=1; i<=n; i++)//Random get length price, but ensure long shorter than the value of high {do{p[i] = (i>4)? (P[i-4] + rand ()%20+1): rand ()%20+1;} while (P[i] <= p[i-1]);p rintf ("%7d", P[i]);} printf ("\ n"); Printcutrodsolution (P, N);//Bottom-up method printf ("\ n"); Memoizedcutrod (P, N);//The top-down method with a memo return 0;} void printcutrodsolution (int p[], int n)//Bottom-up method {int R[max] = {0};//r[i] record maximum value of steel pipe with total length of I int s[max] = {0};//s[i] record r[i] corresponding to Cut length of a section of steel bar int i; Bottomupcutrod (P, R, S, N),//printf ("Maximum value: \ n"),//for (i=1; i<=n; i++)//{//printf ("%2d:%3d", R[i], s[i]);//}//printf ("\ n");p rintf ("Maximum value:%d\n", R[n]);p rintf ("cut scheme: \ n"), while (n > 0) {printf ("%d\t", S[n]), n-= S[n];} printf ("\ n");} void Bottomupcutrod (int p[], int r[], int s[], int n)//record maximum return value of steel tube with total length of I r[i] and cutting length of first segment of bar s[i] {int I, j, max;r[0] = 0;for (i = 1; i<=n; i++)//The total length of the steel pipe is I {max = Infinity;for (j=1; j<=i; J + +) {if (Max < p[j] + r[i-j])//If the decomposition into length J and the length of the I-j two parts can be more profitable, then update the maximum profit value Max, and Record the cut length of the corresponding first segment of steel s[i] {max = P[j] + r[i-j];s[i] = j;}} R[i] = max; The maximum profit value of a steel tube with a total length of I is recorded}}void memoizedcutrod (int p[], int n)//top-down method with memo {int R[max] = {0};//r[i] record the maximum value of the steel pipe with a total length of I int s[max] = {0};//s[i] record r[i] corresponding to the cut length of the first segment of the steel bar int i;for (i=0; i<=n; i++)//Initialize r[i] = infinity;printf ("Maximum value:%d\n", Memoizedcutrodaux ( P, R, S, N));p rintf ("cut scheme: \ n"), while (n > 0) {printf ("%d\t", S[n]), n-= S[n];} printf ("\ n");} int memoizedcutrodaux (int p[], int r[], int s[], int n)//recursively computes the value of r[i], and simultaneously records the value of s[i] {int i, max, temp;if (R[n] >= 0)//has been recorded The return r[n];if (n = = 0) max = 0;else{max = Infinity;for (i=1; i<=n; i++) {temp = P[i] + Memoizedcutroda is no longer repeatedUX (P, R, S, n-i);//calculates the maximum profit value if (Temp > Max)//for which the steel bar is divided into two parts of length I and n-i, and the value of the update maximum return value (S[i]) {max = temp;s[n] = i;}} return r[n] = max;}
An introduction to the problem of steel bar cutting in reading notes