01背包整理 - 2: 模板

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<set>
#include<queue>
#include<deque>
#include<vector>
#include<cmath>
#include<bits/stdc++.h>

#define ll long long
#define debug(____x) cout << ____x << endl
#define readfrom(____s) freopen(____s, "r", stdin)
#define wrtto(____s) freopen(____s, "w", stdout)

using namespace std;

const int desinf=0x3f3f3f3f;
const int inf=2147483647;
const int maxn=1234;

int n; //物品个数 
int v; //背包总容量 

int c[maxn];
int w[maxn];

int dp[maxn];

int main ()
{
    cin >> n >> v;
    for (int i = 0; i < n; i ++)
        cin >> c[i] >> w[i]; //c: 占用位置, w: 钱数 
    for (int i = 0; i < n; i ++)
        for (int j = v; j > 0; j --)
            if(j - c[i] >= 0)
                dp[j] = max (dp[j],  dp[j - c[i]] + w[i]);
    cout << dp[v] << endl;
}

猜你喜欢

转载自www.cnblogs.com/William-Han/p/10159647.html