HDU-2602 Bone Collector 01背包

01背包最裸模板题

#include <cstdlib>
#include <cstring>
#include <cstdio>
#define MAXN 1005
using namespace std;

int N, V, p[MAXN], v[MAXN], dp[MAXN];

inline int max(int x, int y)
{
    return x > y ? x : y;
}

void zobag(int x)
{ 
    for (int i = V; i >= v[x]; --i) { 
        dp[i] = max(dp[i], dp[i-v[x]]+p[x]);
    }
}

int DP()
{
    for (int i = 1; i <= N; ++i) {
        zobag(i);
    }
    return dp[V];
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
        memset(dp, 0, sizeof (dp));
        scanf("%d %d", &N, &V);
        for (int i = 1; i <= N; ++i) { 
            scanf("%d", &p[i]);
        }
        for (int i = 1; i <= N; ++i) {
            scanf("%d", &v[i]);
        }
        printf("%d\n", DP()); 
    }
    return 0;
}
posted @ 2012-04-18 17:11  沐阳  阅读(160)  评论(0编辑  收藏  举报