01背包例题集

eg.1
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602
直接是裸的模板
AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;

int t, n, v, f[N], va[N], vo[N];//v是他背包的体积 

int main(){
    
    
    cin >> t;
    while(t--){
    
    
        cin >> n >> v;
        
        for (int i = 1; i <= n; ++i){
    
    
            cin >> va[i]; 
        }
        for (int i = 1; i <= n; ++i){
    
    
            cin >> vo[i];
        }
        
        memset(f, 0, sizeof f);
        
        for (int i = 1; i <= n; ++i){
    
    
            for (int j = v; j >= vo[i]; --j){
    
    
                f[j] = f[j];
                f[j] = max(f[j], f[j - vo[i]] + va[i]);
            }
        }
        
        cout << f[v] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LXC_007/article/details/114023594
今日推荐