Question: cut a piece of wood with a length of L into n + 1 segments. The cut cost is the length of the current segment.
Analysis: DP, range dynamic planning. The inverse process of stone merge.
State: If f (I, j) is cut from point I to J length into a corresponding small cost, there is a transfer equation:
F (I, j) = min (f (I, k) + f (K, j) + cost (I, j) {where I <k <j };
Note that the DP process is performed in ascending order of the interval size.
Note: Pay attention to the boundary condition. The price is 0 for only a short period of time.
# Include <iostream> # include <cstdlib> # include <cstring> using namespace STD; int cut [55]; int DP [55] [55]; int main () {int L, N; while (CIN> L> N) {for (INT I = 1; I <= N; ++ I) cin> cut [I]; cut [0] = 0; cut [n + 1] = L; // initialization, boundary condition for (INT I = 0; I <55; ++ I) for (Int J = 0; j <55; ++ J) DP [I] [J] = 1000000; For (INT I = 0; I <= N; ++ I) DP [I] [I + 1] = 0; // interval DP for (INT d = 2; D <= n + 1; ++ D) for (int s = 0; S + D <= n + 1; ++ s) for (int K = S + 1; k <S + D; ++ K) if (DP [s] [S + D]> DP [s] [k] + dp [k] [S + D] + cut [S + D]-cut [s ]) DP [s] [S + D] = DP [s] [k] + dp [k] [S + D] + cut [S + D]-cut [s]; cout <"The minimum cutting is" <DP [0] [n + 1] <". "<Endl;} return 0 ;}
Ultraviolet (a) 10003-cutting sticks