题目链接
对于普通的01背包,我们知道,物品只有0和1两种状态,即取或者不取,费用分别是w和0,价值是v和0,而对于这个题来讲,我们把每一段当成一个物品,这样每个物品有3中状态,价值分别为t1,t2,t3。费用本别为 f1,0,f2。背包容量为选手的最大体力值。这样转移方程为:
dp[i+1][j] = min(dp[i+1][j], dp[i][j] + t2[i]);
if (j >= f1[i])
dp[i+1][j-f1[i]] = min(dp[i+1][j-f1[i]], dp[i][j] + t1[i]);
dp[i+1][j + f2[i] > m?m:j + f2[i]] = min(dp[i+1][j + f2[i] > m?m:j + f2[i]], dp[i][j] + t3[i]);
这里要着重注意的是,体力的溢出处理:dp[i+1][j + f2[i] > m?m:j + f2[i]] 由慢速状态恢复的体力,可能溢出!我就是因为这个。。。。。。唉,自闭了。还有就是非法状态的判定,在每次状态转移之前,都有:
if (dp[i][j] == 0x7ffffff) continue;
下面是ac代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#define ll long long
using namespace std;
int dp[128][128];
int t1[128], t2[128], t3[128], f1[128], f2[128];
int main()
{
int t;
cin >>t;
while(t--)
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= m; j++)
dp[i][j] = 0x7ffffff;
dp[1][m] = 0;
int _min = 0x7ffffff;
for (int i = 1; i <= n; i++)
{
scanf("%d%d%d%d%d",&t1[i], &t2[i], &t3[i], &f1[i], &f2[i]);
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
if (dp[i][j] == 0x7ffffff) continue;
dp[i+1][j] = min(dp[i+1][j], dp[i][j] + t2[i]);
if (j >= f1[i])
dp[i+1][j-f1[i]] = min(dp[i+1][j-f1[i]], dp[i][j] + t1[i]);
dp[i+1][j + f2[i] > m?m:j + f2[i]] = min(dp[i+1][j + f2[i] > m?m:j + f2[i]], dp[i][j] + t3[i]);
}
}
/*
for (int i = 1; i <= n + 1; i++)
{
for (int j = 0; j <= m; j++)
cout << dp[i][j] << " ";
cout << endl;
}
*/
for (int i = 0; i <= m; i++)
_min = min(dp[n+1][i], _min);
printf("%d\n", _min);
}
return 0;
}
ps.似乎用记忆化搜索比较清晰???