There is a SuperSale in a SuperHiperMarket. Every person can take only one object of each kind, i.e. one TV, one carrot, but for extra low price. We are going with a whole family to that SuperHiperMarket. Every person can take as many objects, as he/she can carry out from the SuperSale. We have given list of objects with prices and their weight. We also know, what is the maximum weight that every person can stand. What is the maximal value of objects we can buy at SuperSale?ios
The input consists of T test cases. The number of them (1<=T<=1000) is given on the first line of the input file.ide
Each test case begins with a line containing a single integer number N that indicates the number of objects (1 <= N <= 1000). Then follows Nlines, each containing two integers: P and W. The first integer (1<=P<=100) corresponds to the price of object. The second integer (1<=W<=30) corresponds to the weight of object. Next line contains one integer (1<=G<=100) it’s the number of people in our group. Next G lines contains maximal weight (1<=MW<=30) that can stand this i-th person from our family (1<=i<=G).this
For every test case your program has to determine one integer. Print out the maximal value of goods which we can buy with that family.spa
2code
3blog
72 17ip
44 23ci
31 24input
1string
26
6
64 26
85 22
52 4
99 18
39 13
54 9
4
23
20
20
26
72
514
01揹包問題,惟一有點變形的就是這道題中是一羣人組團去的,只要對每一個人作01揹包的動態規劃,最後再求和便可
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int n,g,mw; 8 int p[1050],w[1050]; 9 int dp[35]; 10 11 int main() 12 { 13 int kase; 14 15 scanf("%d",&kase); 16 17 while(kase--) 18 { 19 scanf("%d",&n); 20 for(int i=0;i<n;i++) 21 scanf("%d %d",&p[i],&w[i]); 22 scanf("%d",&g); 23 24 int total=0; 25 for(int k=0;k<g;k++) 26 { 27 scanf("%d",&mw); 28 memset(dp,0,sizeof(dp)); 29 for(int i=0;i<n;i++) 30 for(int j=mw;j>=w[i];j--) 31 if(dp[j-w[i]]+p[i]>dp[j]) 32 dp[j]=dp[j-w[i]]+p[i]; 33 int Max=-1; 34 for(int i=0;i<=mw;i++) 35 if(dp[i]>Max) 36 Max=dp[i]; 37 total+=Max; 38 } 39 40 printf("%d\n",total); 41 } 42 43 return 0; 44 }