Luo Gu P1757 Babel grouping backpack

P1757 Babel grouping backpack

Topic Link - grouping backpack Babel
Here Insert Picture Description
problem-solving ideas
typical packet knapsack problem

  • A backpack-dimensional pseudo-code packet
for 所有的组k
    for v=V..0
        for 所有的i属于组k
      f[v]=max{f[v],f[v-w[i]]+c[i]}

The same triple loop, but needs to be recorded when the set number of z input
array T [] is used to record a few items each
two-dimensional array S [] [] i of each record of the number of items

Attach Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int INF=0x3f3f3f;
int a[1010],b[1010],c;
int dp[1010],s[1010][1010],t[110];
int main(){
 ios::sync_with_stdio(0);
 cin.tie(0);cout.tie(0);
 
 int m,n;
 cin>>m>>n;
 int z=-1;
 for(int i=1;i<=n;i++){
  cin>>a[i]>>b[i]>>c;
  z=max(z,c);
  t[c]++;
  s[c][t[c]]=i;
 }
 for(int i=1;i<=z;i++){//枚举每个组
  for(int j=m;j>=0;j--){//枚举容量
   for(int k=1;k<=t[i];k++){//枚举各组中物品序号
    if(j>=a[s[i][k]])
     dp[j]=max(dp[j],dp[j-a[s[i][k]]]+b[s[i][k]]);
   }
  }
 }
 cout<<dp[m]<<endl;
 return 0;
}
Published 12 original articles · won praise 3 · Views 8939

Guess you like

Origin blog.csdn.net/Fiveneves/article/details/104102948