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