【动态规划】01背包

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 6;
const int W = 12;
int x[N];  

int main()
{

	int weight[N] = {4, 6, 2, 2, 5, 1};
	int value[N] = {8, 10, 6, 3, 7, 2};
	int dp[N+1][W+1] = {{0}};
	int b[N+1][W+1];
	int i, j;
	for(i=1 ; i<= N ; i++)
	{
		for(j=1; j<=W ; j++)
		{
			if(j >= weight[i-1]){		
				dp[i][j] = max(dp[i-1][j] , dp[i-1][j-weight[i-1]] + value[i-1]);      
				b[i][j]=1;
			}
			else dp[i][j] =	dp[i-1][j];		   
			cout<<dp[i][j]<<' ';
		}
		cout<<endl;
	}
	cout<<dp[N][W]<<endl; 
	
    return 0;
}

阅读更多

没有更多推荐了,返回首页