FATE
10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
0 -1 1
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cctype>
#define INF 0x3f3f3f3f
#define maxn 100+10
using namespace std;
int cost[maxn],val[maxn],cnt[maxn];
int dp[maxn];
int n,m,k,s;
void completepack()
{
memset(cnt,0,sizeof cnt);
memset(dp,0,sizeof(dp));
for(int i=1;i<=k;i++)
for(int j=cost[i];j<=m;j++)
{
if(dp[j]<dp[j-cost[i]]+val[i])
{
cnt[j]=cnt[j-cost[i]]+1; //计数数组
dp[j]=dp[j-cost[i]]+val[i];
}
}
}
int main()
{
while(scanf("%d%d%d%d",&n,&m,&k,&s)!=EOF)
{
int ok=1;
for(int i=1;i<=k;i++)
scanf("%d%d",val+i,cost+i);
completepack();
for(int i=0;i<=m;i++) //这里貌似是题目的bug,经验可以为0.
{
if(dp[i]>=n&&cnt[i]<=s)
{
printf("%d\n",m-i);
ok=0;
break;
}
}
if(ok)
printf("-1\n");
}
return 0;
}
HDU FATE (完全背包+有限取次)(二重费用背包),布布扣,bubuko.com
原文地址:http://blog.csdn.net/code_or_code/article/details/26411709