您的位置:首页 > 其它

NYOJ 289 (0-1背包)

2014-03-23 20:49 225 查看
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAXN = 1010;

int MAX(int a, int b)
{
return a > b ? a : b;
}

int dp[MAXN];
int V;

void Zero_One_Pack(int cost, int Price)
{
for(int v = V; v >= cost; --v)
dp[v] = MAX(dp[v], dp[v - cost] + Price);
}

int main()
{
int n;
int cost, price;
while(~scanf("%d %d", &n, &V) && n && V)
{
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; ++i)
{
scanf("%d %d", &cost, &price);
Zero_One_Pack(cost, price);
}
cout<<dp[V]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: