0 1 Backpack Continuation, consider a recursive relationship

Source: Internet
Author: User

Because W is too big. If DP[I][J]: To select the item I, the weight does not exceed the value of J items, the second cycle is too expensive to reconsider the recurrence relationship, Dp[i][j] means to select the I item, the value of the minimum weight J
1 intdp[ the][10005];2 3 intv[ the];4 intw[ the];5 6 intMain () {7     8     intn,w;9      while(SCANF ("%d", &n)! = EOF &&N) {Ten          for(inti =0; I < n; i + +){ Onescanf"%d%d",&w[i],&v[i]); A         } -scanf"%d",&W);  -          thememset (dp[0],0x7f7f7f7f,sizeof(dp[0])); -dp[0][0] =0; -         /* - Dp[i][j] Indicates the minimum weight of the I item, the value of J +         */ -          for(inti =0; I < n; i + +){ +              for(intj =0; J <= -* -; J + +){ A                 if(J <V[i]) { atdp[i+1][J] =Dp[i][j]; -}Else{ -dp[i+1][j] = min (Dp[i][j],dp[i][j-v[i]] +w[i]); -                 } -             } -         }  in          -         intAns =0; to          for(inti =0; I <=10000; i + +){ +             if(Dp[n][i] <= W) ans =i; -         } theprintf"%d\n", ans); *          $     }Panax Notoginseng     return 0; - } the /* + 4 A 2 3 the 1 2 + 3 4 - 5 $  $ 7 - */

0 1 Backpack Continuation, consider a recursive relationship

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.