第四讲 分组背包问题

01背包只不过是  每一组的个数为1,分组背包每一组的个数为s[i]

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N=110;
 5 int f[N];
 6 int v[N][N],w[N][N],s[N];
 7 int n,m,k;
 8 
 9 int main()
10 {
11     cin>>n>>m;
12     for(int i=1;i<=n;i++)
13     {
14         cin>>s[i];
15         for(int j=1;j<=s[i];j++)
16         {
17             cin>>v[i][j]>>w[i][j];
18         }
19     }
20 
21     for(int i=1;i<=n;i++)//哪一组
22     {
23         for(int j=m;j>=1;j--)//容量
24         {
25             //01背包只不过是  每一组的个数为1,分组背包每一组的个数为s[i]
26             for(int k=1;k<=s[i];k++)//对每一组的个数进行枚举
27             {    
28                 if(j>=v[i][k]) f[j]=max(f[j],f[j-v[i][k]]+w[i][k]);  
29             }
30         }
31     }
32     cout<<f[m]<<endl;
33     return 0;
34 }

猜你喜欢

转载自www.cnblogs.com/yuhong1103/p/12522498.html