http://acm.nyist.net/JudgeOnline/problem.php?pid=860
很经典的一道背包题目,一般情况,我们都是把背包的容量作为数组的变量,但是这一道题,背包容量数据量太大了。。。。10^9
把容量和价值进行互换
#include"iostream"
#include"stdio.h"
#include"string"
#include"cstring"
#include"cmath"
#include"algorithm"
#include"stdlib.h"
using namespace std;
int f[20000];
int weight[200],cost[200];
int main()
{
int n,w;
while(cin>>n>>w)
{
int i,j,sum = 0;
memset(f,'a',sizeof(f));
f[0] = 0;
for(i = 1; i <= n; i++)
cin>>weight[i]>>cost[i],sum+=cost[i];
for(i = 1; i <= n; i++)
for(j = sum; j >= cost[i]; j--)
{
f[j] = min(f[j],f[j-cost[i]]+weight[i]);
}
int maxx = 0;
for(i = sum; i >=0; i--)
if(f[i]<=w) {maxx = i;break;}
cout<<maxx<<endl;
}
return 0;
}