标签:style blog class code java color
/** 01背包,recursive * 05.08/2014 */ #include <cstdio> #include <cstring> #include <algorithm> #define MAXN 30000 using namespace std; int N,W; int w[MAXN],v[MAXN]; int solve(int i, int tw) { int res; if( i == N) //已经全部搜索完 res = 0; else if( tw < w[i]) res = solve(i+1,tw);//当前物品太重,搜索下一个 else res = max(solve(i+1,tw),solve(i+1,tw - w[i]) + v[i]); //当前物品可以放入背包,则可以选择1:不放入,搜索下一个,或者2:放入,加入其价值 return res; } int main() { scanf("%d",&N); for(int i = 0; i < N; i++) scanf("%d%d",&w[i],&v[i]); scanf("%d",&W); printf("%d",solve(0,W)); return 0; }
标签:style blog class code java color
原文地址:http://www.cnblogs.com/difei/p/3715862.html