01 背包
做过好几次了吧
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<stack> #define mem(a,b) memset(a,b,sizeof(a)) #define ll __int64 #define MAXN 1000 #define INF 0x7ffffff #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; int dp[13880]; int main() { int n,m; int i,j; int w,d; int maxx; while(scanf("%d%d",&n,&m)!=EOF) { mem(dp,-1); dp[0]=0;maxx=0; while(n--) { scanf("%d%d",&w,&d); for(i=m;i>=w;i--) { if(dp[i-w]!=-1&&dp[i-w]+d>dp[i]) { dp[i]=dp[i-w]+d; if(dp[i]>maxx) maxx=dp[i]; } } } printf("%d\n",maxx); } return 0; }
POJ 3624 Charm Bracelet,布布扣,bubuko.com
原文:http://www.cnblogs.com/sola1994/p/3923650.html