0-1背包问题(dfs记忆化搜索写法)

#include 
#include 
#include 
using namespace std;

int n,v;
int p[51], w[51], dp[51][51];

int dfs(int i, int v)
{
	if(dp[i][v]!=0) return dp[i][v];
	if(i==0||v<=0) return 0;
	if(w[i]>v)
		dp[i][v] = dfs(i-1,v);
	else
		dp[i][v] = max(dfs(i-1,v), dfs(i-1,v-w[i])+p[i]);
	
	return dp[i][v];
}

int main()
{
	while(scanf("%d%d",&n,&v)!=EOF)
	{
		memset(dp,0,sizeof(dp));
		for(int i=0;i>p[i];
		for(int i=0;i>w[i];
		cout<

你可能感兴趣的:(动态规划,C++,算法,动态规划)