Multiple backpack Hdu-1171big Event in HDU &hdu-2191 mourning 512 Wenchuan earthquake--cherish now, Thanksgiving life

Source: Internet
Author: User

These two questions are the basic problem of multiple backpacks, the front of the anchor test instructions is: give the value of each object and the number of objects, how to divide so that the value of a A, the value of a can not be less than B, similar to the Nyoj on the stamp of your half that meaning, but here is not one but many, So multiple backpacks

The previous question is to use half of the sum as the capacity of the backpack to ask, the code is as follows

1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intdp[100000];8 intcnt[ -];9 intvalue[ -];Ten intMain () One { A     intN; -      while(~SCANF ("%d", &n) && n >0) -     { theMemset (DP,0,sizeof(DP)); -         intsum =0; -          for(inti =0; I < n; i++) -         { +scanf"%d%d", &value[i], &cnt[i]); -Sum + = value[i] *Cnt[i]; +         } A         intv = SUM/2; at          for(inti =0; I < n; i++) -         { -              for(intj = V; J >= Value[i]; j--)//01 Backpack -             { -                  for(intK =1; K <= Cnt[i] && k * Value[i] <= J; k++)//Traverse each of the -DP[J] = max (Dp[j], dp[j-k * Value[i]] + k *value[i]); in             } -         } toprintf"%d%d\n", Sum-Dp[v], dp[v]); +     } -     return 0; the}

The second question is the same, and it's a multiple backpack.

The code is as follows

1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 Const intN = Max;7 intWeight[n];8 intValue[n];9 intCnt[n];Ten intDp[n]; One intMain () A { -     intT; -     intN, Money; thescanf"%d", &T); -      while(t--) -     { -scanf"%d%d", &money, &n); +          for(inti =0; I < n; i++) -scanf" %d%d%d", &weight[i], &value[i], &cnt[i]); +Memset (DP,0,sizeof(DP)); A          for(inti =0; I < n; i++) at         { -              for(intj = money; J >= Weight[i]; j--)//01 Backpack -             { -                  for(intK =1; K <= Cnt[i] && k * Weight[i] <= money; k++)//Traverse each case, so the top is 01 backpack, if the top with a full backpack, it is equivalent to take weight -                     if(J >= K *Weight[i]) -DP[J] = max (Dp[j], dp[j-k * Weight[i]] + k *value[i]); in             } -         } toprintf"%d\n", Dp[money]); +     } -     return 0; the}

Multiple backpack Hdu-1171big Event in HDU &hdu-2191 mourning 512 Wenchuan earthquake--cherish now, Thanksgiving life

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.