UV-10003-Cutting Sticks (interval DP)
Here we will briefly describe the interval DP:
Main ideas:
The interval dynamic planning problem is generally considered for each interval. Their Optimal Values are obtained from the Optimal Values of several smaller intervals and are an application of the divide and conquer idea, divide an interval problem into smaller intervals until the intervals of a prime component, enumerate their combinations, and obtain the optimal value after merging.
Define the status: Set dp [I] [j] as the minimum price between interval I and j (actually, let's look at the question)
Implementation process:
For (int p = 1; p <= n; p ++) {// p is the length of the interval, as a stage
For (int I = 1; I <= n; I ++) {// I is the starting point of the exhaustive range
Int j = I + p-1; // j indicates the end point of the interval.
For (int k = I; k <j; k ++) // status transfer
Dp [I] [j] = min {dp [I] [k] + dp [k + 1] [j] + w [I] [j]}; // This is what the question means. Some of them need to start from k, not k + 1.
Dp [I] [j] = max {dp [I] [k] + dp [k + 1] [j] + w [I] [j]};
}
}
For this question, we must first define the state transition equation as dp [I] [j] = min (dp [I] [k], dp [k] [j]). + num [j]-num [I] (I
AC code:
# Include
# Include
# Include
# Include
# Define INF 0x3fffffffusing namespace std; const int maxn = 60; int dp [maxn] [maxn]; // dp [I] [j] indicates that the interval I, minimum Cost between j: int num [maxn]; int main () {int L, n; while (scanf ("% d", & L) {scanf ("% d", & n); for (int I = 1; I <= n; I ++) scanf ("% d ", & num [I]); num [0] = 0; num [n + 1] = L; memset (dp, 0, sizeof (dp )); for (int p = 1; p <= n + 1; p ++) for (int I = 0; I <= n + 1; I ++) {int j = I + p; int MIN = INF; if (j> n + 1) break; f Or (int k = I + 1; k <j; k ++) {int tmp = dp [I] [k] + dp [k] [j] + num [j]-num [I]; MIN = min (MIN, tmp );} if (MIN! = INF) dp [I] [j] = MIN;} printf ("The minimum cutting is % d. \ n ", dp [0] [n + 1]);} return 0 ;}