After all the hardships, I finally figured out the complete backpack. From this, I am not very familiar with the dynamic planning sub-structure. Come on !!! Believe in yourself! I can...
Question:
Select a coin to fill the bottle (w) with the minimum value.
Understanding the question is a complete backpack problem.
For full backpacks,
Two-dimensional solution:
Assuming DP [I] [J], I indicates that the weight of the I coin is J. The sub-structure is DP [I-1] [J-W [J] + P [J] (put a coin less, the weight is the current weight minus the weight of the coin ),
The smallest value in DP [I] [W] is the solution.
Since each coin can be placed many times, we need to add an extra cycle. W [J] is decomposed into (K * W [J]), P [J] is decomposed into K * P [J] (1 = <k <= w/w [J]), that is, each coin is divided into many coins with the same weight value.
Initialization problem. When w = 0, DP [I] [0] = 0;
In addition, is this solution similar to multiple backpacks!
One-dimensional solution:
We can find that the J-W [I] of DP [I] [J-W [I] represents the state of I-1. The relationship between DP [J] and DP [J-W [I] corresponds to the relationship between I and I-1. DP [J] = min (DP [J], DP [J-W [I-1] + P [I-1]);
Initialization, DP [0] [0] = 0;
Optimization of one-dimensional solutions:
Since DP [I] [J], I indicates that the weight of the I coin is J, and the I-1 is less than one, assuming that no matter whether or not the previous release, that is, no matter how many coins have been placed, you can continue to place them. Simplified to a truly complete backpack (the difference between a 0-1 backpack and a 0-1 backpack is that a 0-1 backpack can only be placed, and the ones that have been put before can no longer be placed, so N-1 is required)
State transition equation: DP [I] = min (DP [I], DP [I-W [I] + P [I]);
Code:
# Include <stdio. h >#include <algorithm> # include <iostream> using namespace STD; struct info {int P; int W;} n [505]; int DP [10001]; // item weight J // is it feasible to find J as a value, and can output the smallest valueint min (int A, int B) {return A> B? B: A;} int main () {int t; scanf ("% d", & T); While (t --) {int I, J, K; int min = 100000000; scanf ("% d", & I, & J); int W = J-I; int N; scanf ("% d ", & N); for (I = 0; I <n; I ++) scanf ("% d", & N [I]. p, & N [I]. w);/* the two-dimensional solution is likely to time out for (I = 0; I <= N; I ++) for (j = 1; j <= W; j ++) {DP [I] [0] = 0; DP [I] [J] = 100000000 ;}for (I = 1; I <= N; I ++) {for (j = N [I-1]. w; j <= W; j ++) {for (k = N [I-1]. w; k <= J; k + = N [I-1]. w) {DP [I] [J] = min (DP [I] [J], DP [I-1] [J-K] + k/n [I-1]. W * n [I-1]. p); If (j = W & min> DP [I] [J]) min = DP [I] [J] ;}} * /// simplified from two-dimensional to one-dimensional array/* For (j = 1; j <= W; j ++) DP [J] = 100000000; DP [0] = 0; for (I = 1; I <= N; I ++) {for (j = N [I-1]. w; j <= W; j ++) {for (k = N [I-1]. w; k <= J; k + = N [I-1]. w) {DP [J] = min (DP [J], DP [J-K] + k/n [I-1]. W * n [I-1]. p) ;}}*/For (j = 1; j <= W; j ++) DP [J] = 100000000; DP [0] = 0; for (I = 0; I <n; I ++) {for (j = N [I]. w; j <= W; j ++) DP [J] = min (DP [J], DP [J-N [I]. w] + N [I]. p);} If (DP [w] <100000000) printf ("the minimum amount of money in the piggy-bank is % d. \ n ", DP [w]); else printf (" This is impossible. \ n ") ;}return 0 ;}
I wrote another question about multiple backpacks, which is similar to a full backpack.
A complete backpack .... Multiple small cases (only this question ,,,,)
Question:
I do not need to speak Chinese, and do not want to explain.
For details, see the code.
# Include <stdio. h >#include <algorithm> # include <iostream> using namespace STD; struct info {int P; // weight int W; // price int C; // quantity} n [105]; int DP [101] [101]; // place an I-type item with a heavy jint max (int A, int B) {return A> B? A: B;} int main () {int t; scanf ("% d", & T); While (t --) {int I, j, k; int W, n; int min = 0; scanf ("% d", & W, & N); for (I = 0; I <n; I ++) scanf ("% d", & N [I]. w, & N [I]. p, & N [I]. c); for (I = 0; I <= N; I ++) for (j = 1; j <= W; j ++) DP [I] [J] = 0; for (I = 1; I <= N; I ++) {for (j = 1; j <= W; j ++) {for (k = 0; k <= N [I-1]. c; k ++) {If (j> = K * n [I-1]. w) DP [I] [J] = max (DP [I] [J], DP [I-1] [J-K * n [I-1]. w] + K * n [I-1]. p); If (Min <DP [I] [J]) min = DP [I] [J] ;}} printf ("% d \ n ", min);} return 0 ;}