HDOJ-2159 FATE

这道题是二维费用的完全背包问题.两种费用支付的最大值分别为,忍耐度和最多杀怪数.
状态转移方程为: dp[j][h] = max(dp[j][h], dp[j-pa[i]][h-1] + expe[i]);

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

int dp[105][105];
int expe[105], pa[105];

int main()
{
   // freopen("in.txt", "r", stdin);
    int n, m, k, s;

    while(cin >> n >> m >> k >> s)
    {
        for(int i = 0; i < k; i++)
            cin >> expe[i] >> pa[i];
        memset(dp, 0, sizeof(dp));

        for(int i = 0; i < k; i++)
            for(int j = pa[i]; j <= m; j++)
            for(int h = 1; h <= s; h++)
            dp[j][h] = max(dp[j][h], dp[j-pa[i]][h-1] + expe[i]);
        int maxs = -1;
        for(int i = 0; i <= m; i++)
            for(int j = 0; j <= s; j++)
            {
                if(dp[i][j] >= n)
                    maxs = max(maxs, m - i);
            }
        cout << maxs << endl;
    }
    return 0;
}

你可能感兴趣的:(HDOJ-2159 FATE)