本人刚入门dp,水平较菜,因此记录自己对于此题的代码思路,希望能帮到刚入门的同学,也为自己复习留一份记录。
朴素版算法:
#include <iostream>
using namespace std;
const int N = 1010;
int v[N], w[N];
int dp[N][N];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
{
cin >> v[i] >> w[i];
}
for (int i = 1; i <= n; i ++ )
{
for (int j = 0; j <= m; j ++ )
{
for(int k = 0; k * v[i] <= j; k ++ )
{
dp[i][j] = max(dp[i][j], dp[i - 1][j - k * v[i]] + k * w[i]);
}
}
}
cout << dp[n][m] << endl;
return 0;
}
不难发现朴素算法时间复杂度较高。
优化思路:对状态转移方程进行改进
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - k * v[i]] + k * w[i]);
dp[i][j - v[i]] = max(dp[i - 1][j - v[i]], dp[i - 1][j - (k - 1) * v[i]] + k * w[i]);
其中k = 1, 2, 3....
上下做差可得
dp[i][j] = max(dp[i][j - v[i]] + w[i], dp[i - 1][j])
上式即为新状态转移方程。
改进代码:
#include <iostream>
using namespace std;
const int N = 1010;
int v[N], w[N];
int dp[N][N];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
{
cin >> v[i] >> w[i];
}
for (int i = 1; i <= n; i ++ )
{
for (int j = 0; j <= m; j ++ )
{
dp[i][j] = dp[i - 1][j];
if(j >= v[i])
dp[i][j] = max(dp[i][j - v[i]] + w[i], dp[i][j]);
}
}
cout << dp[n][m] << endl;
return 0;
}
观察到上述代码与01背包问题高度相似,因此可优化为一维:
#include <iostream>
using namespace std;
const int N = 1010;
int v[N], w[N];
int dp[N];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
{
cin >> v[i] >> w[i];
}
for (int i = 1; i <= n; i ++ )
{
for (int j = 0; j <= m; j ++ )//此处由于均使用i- 1维,因此不需要从大到小枚举
{
if(j >= v[i])
dp[j] = max(dp[j - v[i]] + w[i], dp[j]);
}
}
cout << dp[m] << endl;
return 0;
}