This problem is similar to matrix multiplication, which is the question of interval. Set DP[I][J] represents the minimum cost from I to J, then Dp[i][j]=min{dp[i]
[K]+dp[k][j]+a[j]-[i]} (I<K<J) where a[j]-a[i] represents the length from I to J, which is the cost required to cut this piece of wood. Seeking large areas
The time between the community has been calculated, so in line with the dynamic planning of the bottom-up, but also the optimal substructure, the problem I put 0 and the length of the wood bar to a
Inside the array, that is, there are n+2 points, each two adjacent points without cutting, initialized to 1
Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <vector> #include <set> #include <string> #include <algorithm>using namespace std; int main () {int s,n;int dp[55][55];int a[55];int i,j,r,k,t,m;while (cin >> s,s) {cin >> n;a[1] = 0;a[n+2] = s;for (i=2; i<=n+1; i++) cin >> a[i];m = n+2;for (i=1; i<m; i++) dp[i][i+1] = 0;for (r=2; r<=m-1; r++)//r indicates the length of the interval, with a minimum of 2 , the longest is m-1 {for (i=1; i<=m-r; i++)//i represents the beginning of the interval, the minimum is 1, the longest is based on R to find out {j=i+r;//j indicates the midpoint of the interval dp[i][j] = dp[i+1][j] +dp[i][i+1] + a[j]-a[i ];for (k=i+2; k<j; k++) {t = Dp[i][k] + dp[k][j] + a[j]-a[i];if (T < dp[i][j]) dp[i][j] = t;}}} cout << "The minimum cutting is" << dp[1][m] << '. ' << Endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 10003 cutting sticks cut wood strips DP