题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214
题面是最正常的背包,但是背包重量大到让人思考人生。
说真的第一看见这样的题目,比赛的时候看见空间这么大顿时懵逼了。真是无从下手。然后慢慢的就想到了背的是价值不是空间,然后就出来了,但是边界情况一定要处理好,赛场的时候因为边界问题RE了好几次
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
int t;
int n;
long long m;
long long dp[5005];
long long weight[5005];
int value[5005];
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
int sumv=0;
memset(dp , inf , sizeof(dp));
for(int i=0; i<n; i++)
{
cin>>weight[i]>>value[i];
sumv+=value[i];
}
dp[0]=0;
for(int i=0; i<n; i++)
{
for(int j=sumv; j>=value[i]; j--)
dp[j]=min(dp[j],dp[j-value[i]]+weight[i]);
}
for(int ans=sumv; ans>=0; ans--)
{
if(dp[ans]<=m)
{
cout<<ans<<endl;
break;
}
}
}
return 0;
}