01背包模板 及 优化

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背包 二维模板
#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[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=n;j>=w[i];j--) // things;
        {
            dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
        }
    }cout<<dp[n];
}
01背包 一维模板

 2 : 完全背包

#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][j-w[i]]+v[i]);
            else
            dp[i][j]=dp[i-1][j];
        }
    }cout<<dp[m][n];
}
完全背包 二维模板

和01背包有所区别   

#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[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[j]=max(dp[j],dp[j-w[i]]+v[i]);
            else
            dp[j]=dp[j];
        }
    }cout<<dp[n];
}
完全背包 一维模板
原文地址:https://www.cnblogs.com/Andromeda-Galaxy/p/9428506.html