Full backpack (contains a condition that does not fit exactly)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.