我的背包

来源:互联网 发布:免费的光盘刻录软件 编辑:程序博客网 时间:2024/05/02 21:01

#include <iostream>
using namespace std;
#define MAXSIZE 205
int f[MAXSIZE + 1];
//int c[MAXSIZE + 1]={0,79,58,86,11,28,62,15,68};
//int w[MAXSIZE + 1]={0,83,14,54,79,72,52,48,62};
int c[MAXSIZE + 1]={0,77,22,29,50,99};
int w[MAXSIZE + 1]={0,92,22,87,46,90};
int main( )
{
 int N,V;
 int i = 1;
 //N=8,V=200;
 N=5,V=100;
 for (i = 1; i <= N; ++i)
 {
  for (int v = V; v >= c[i]; --v) //c[i]可优化为bound,bound = max {V - sum c[i,...n],c[i]}
  {
   f[v] = (f[v] > f[v - c[i]] + w[i]?f[v] : f[v - c[i]] + w[i]);
  }
 }
 //当i=N时,可以跳出循环单独计算F[V]
 cout << f[V] << '\n';
 return 0;
}

 

 

原创粉丝点击