HDU 2159 FATE

二维费用的背包

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=105;
const int INF=0x7FFFFFFF;
int value[maxn],cost[maxn];
int dp[maxn][maxn];
int main()
{
    int n,m,k,s;
    int b,i,j;
    int ans;
    while(~scanf("%d%d%d%d",&n,&m,&k,&s))
    {
        for(i=1; i<=k; i++)
            scanf("%d%d",&value[i],&cost[i]);
        for(i=0; i<=m; i++)
            for(j=0; j<=s; j++)
                dp[i][j]=0;
        for(i=1; i<=k; i++)
            for(j=cost[i]; j<=m; j++)
                for(b=1; b<=s; b++)
                    dp[j][b]=max(dp[j][b],dp[j-cost[i]][b-1]+value[i]);
        ans=INF;
        for(i=0; i<=m; i++)
            for(j=0; j<=s; j++)
                if(dp[i][j]>=n&&i<=ans)
                    ans=i;
        if(ans==INF)
            printf("%d
",-1);
        else printf("%d
",m-ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/4648630.html