This is the topic of our 15-level freshman warm-up, when few freshmen came, not only because they did not learn DP, but also because it is not a small white full backpack, here is given a value of M, let us choose certain items to make their value >=m, let us ask for the minimum cost.
First of all clear M is not as a backpack capacity, because our value may be greater than m, in fact, let us ask for the minimum cost, we are undoubtedly in the beginning to initialize the DP to infinity, so naturally give a mark, so we have to find that exactly fill the point, the point value is the answer we want, In fact, the method is very simple, as long as the appropriate amplification of the capacity of the backpack, if the expansion of K, that in (m,m+k) between the order, find the minimum value can be. K Here is the greatest value of these items, because if the outfit is not good enough, it must have been added.
Now look back at the beginning of their own problems, now just write this blog, feel that they really grow, some harvest, so continue to work it ~
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;intMain () {intT; scanf ("%d",&t); while(t--) { intn,m; scanf ("%d%d",&n,&m); intc[ $],w[ $]; Long Longf[2200]; for(inti =1; I <= N; i++) {scanf ("%d%d",&c[i],&W[i]); } for(inti =1; I <= m + +; i++) F[i]=999999999; f[0] =0; for(inti =1; I <= N; i++) { for(intj = W[i]; J <= m + +; J + +) if(F[j-w[i]] + C[i] <F[j]) f[j]= F[j-w[i]] +C[i]; } sort (F+m,f+m+ +); cout<<f[m]<<Endl; }}
Full backpack (contains a condition that does not fit exactly)