hdu 2159 FATE 二维费用的完全背包

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159

 计算dp数组的时候

里面两层for循环调换位置对结果没有影响

“个数成本”一定要从1到max

“花费成本”就看是01背包还是完全背包来决定逆序还是顺序

 最终的目标是耐久度剩余最大 即耐久度花费最小

把两个成本和目标分清楚就没问题了

【为了把他萌分清楚我想了好久Orz】

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 130;
const int INF = 100000;

int a[maxn], b[maxn];
int dp[maxn][maxn];

int main()
{
    //freopen("in.txt", "r", stdin);

    int n, m, k, s;
    while(scanf("%d%d%d%d", &n, &m, &k, &s) == 4)
    {
        for(int i = 1; i <= k; i++)
            scanf("%d%d", &a[i], &b[i]);

        int V = n + 20;
        int ans = INF;

        for(int i = 0; i <= n + 20; i++)
            for(int j = 0; j <= s; j++)
                dp[i][j] = INF;
        dp[0][0] = 0;

        for(int z = 1; z <= k; z++)
            for(int i = a[z]; i < V; i++)
                for(int j = 1; j <= s; j++)
                {
                    dp[i][j] = min(dp[i][j], dp[i - a[z]][j - 1] + b[z]);
                    if(i >= n && dp[i][j] <= m && dp[i][j] < ans)
                        ans = dp[i][j];
                }

        if(ans == INF)
            printf("-1
");
        else
            printf("%d
", m - ans);

    }


    return 0;
}
原文地址:https://www.cnblogs.com/dishu/p/4296190.html