hdu_2159(二维费用背包)
HDU_2159
二维费用背包问题
http://acm.hdu.edu.cn/showproblem.php?pid=2159
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 1e8
int dp[110][110];
int w[110];
int v[110];
int main()
{
int n,m,k,s;
while(~scanf("%d%d%d%d",&n,&m,&k,&s)){
memset(dp,0,sizeof(dp));
for(int i = 0;i < k; i++){
scanf("%d%d",&v[i],&w[i]);
}
for(int i = 0; i < k; i++){
for(int j = w[i]; j <= m; j++){
for(int k = 1; k <= s; k++){
dp[j][k] = max(dp[j][k],dp[j-w[i]][k-1]+v[i]);
}
}
}
int maxj = INF;
for(int i = 0; i <= m; i++){
for(int j = 0; j <= s; j++){
dp[i][j] -= n;
if(dp[i][j]>=0){
maxj = min(maxj,i);
}
}
}
int ans;
if(maxj==INF) ans = -1;
else ans = m-maxj;
printf("%d\n",ans);
}
return 0;
}