hdu 2602 Bone Collector 01背包

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602

 

裸的01背包

 

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 1010;

int w[maxn], c[maxn], f[maxn];
int ans;
int n, V;


void ZeroOnePack(int cost, int weight)
{
    for(int i = V; i >= cost; i--)
    {
        f[i] = max(f[i], f[i-cost] + weight);
        if(f[i] > ans)
            ans = f[i];
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);

    int T;
    scanf("%d", &T);

    while(T--)
    {
        ans = 0;
        memset(f, 0, sizeof(f));

        scanf("%d%d", &n, &V);

        for(int i = 0; i < n; i++)
            scanf("%d", &w[i]);
        for(int i = 0; i < n; i++)
            scanf("%d", &c[i]);

        for(int i = 0; i < n; i++)
            ZeroOnePack(c[i], w[i]);

        printf("%d\n", ans);
    }

    return 0;
}

 

posted @ 2015-02-17 23:27  地鼠地鼠  阅读(116)  评论(0编辑  收藏  举报