NOIP2006 Jin Ming's Budget Plan

Jin Ming's budget plan

 

Obviously a knapsack problem

Put each main component and its corresponding accessories in a group, and enumerate each group. There are the following options:

1. do not choose

2. Only select the main part

3. One main part + one accessory

4. One main part + two accessories

 

So it became the 01 backpack. .

 

1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4  using  namespace std;
 5  int n,m,f[ 32010 ],v[ 65 ][ 4 ],w[ 65 ][ 4 ], pos[ 65 ]; // pos[i] records the subscript of the item number i in the array v, w 
                            //w[i][0] represents the number of attachments of the i-th main piece
6 int main() 7 { 8 scanf( " %d%d " ,&n,& m); 9 int V,P,Q,sum; 10 for(int i=1;i<=m;i++) 11 { 12 scanf("%d%d%d",&V,&P,&Q); 13 if(Q==0) 14 { 15 v[++sum][1]=V; 16 w[sum][1]=P; 17 pos[i]=sum; 18 } 19 else 20 { 21 w[pos[Q]][++w[pos[Q]][0]+1]=P; 22 v[pos[Q]][w[pos[Q]][0]+1]=V; 23 } 24 } 25 for(int i=1;i<=sum;i++) 26 for(int j=n;j>=v[i][1];j--) 27 { 28 f[j]=max(f[j],f[j-v[i][1]]+w[i][1]*v[i][1]); 29 if(w[i][0]>=1&&j-v[i][1]-v[i][2]>=0) 30 f[j]=max(f[j],f[j-v[i][1]-v[i][2]]+w[i][1]*v[i][1]+w[i][2]*v[i][2]); 31 if(w[i][0]==2&&j-v[i][1]-v[i][3]>=0) 32 { 33 f[j]=max(f[j],f[j-v[i][1]-v[i][3]]+w[i][1]*v[i][1]+w[i][3]*v[i][3]); 34 if(j-v[i][1]-v[i][2]-v[i][3]>=0) 35 f[j]=max(f[j],f[j-v[i][1]-v[i][2]-v[i][3]]+w[i][1]*v[i][1]+w[i][2]*v[i][2]+w[i][3]*v[i][3]); 36 } 37 } 38 printf("%d\n",f[n]); 39 return 0; 40 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324469499&siteId=291194637