1:   01背包

模板: 注意是两个for   第一个是物品  第二个是重量 

#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=1e5+10;
const int INF=0x3f3f3f3f;
int w[maxn];
int v[maxn];
int dp[30][maxn];
int32_t main()
{
    int n,m;    cin>>n>>m;
    for(int i=1;i<=m;i++) { cin>>w[i]>>v[i];   }  
    for(int i=1;i<=m;i++) // weight
    {
        for(int j=1;j<=n;j++) // things;
        {
            if(j>=w[i])
            dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
            else
            dp[i][j]=dp[i-1][j];
        }
    }cout<<dp[m][n];
}
01背包 二维模板

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-17
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案