相当裸,练练手了,还有不到一个月了阿.
看DD牛的背包九讲,,,,中.
http://poj.org/problem?id=3624
dp[v]表示容量为V的背包可以装的最大价值.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXI 4005
#define MAXV 24888
int dp[MAXV];
int main()
{
int N,M;
int cost[MAXI],weight[MAXI];
int i,v;
while(~scanf("%d%d",&N,&M))
{
for(i=1;i<=N;i++)
scanf("%d%d",&cost[i],&weight[i]);
memset(dp,0,sizeof(dp));
for(i=1;i<=N;i++)
for(v=M;v>=cost[i];v--)//比如第一个是V1,第二个可以从0推到v2也可以从v1推到v1+v2,这里反过来。
dp[v]=dp[v-cost[i]]+weight[i]>dp[v]?dp[v-cost[i]]+weight[i]:dp[v];
cout<<dp[M]<<endl;
}
return 0;
}