1.背包dp
1.0-1 背包 2.完全背包 3.多重背包 4.二进制分组优化
5.单调队列优化 6.混合背包 7.二维费用背包 8.分组背包
9.有依赖的背包 10.泛化物品的背包 11.杂项小优化 12.背包问题变种
13.输出方案求方案数 14.求最优方案总
1.01-背包(滚动数组优化后的)
// C++ Version
for (int i = 1; i <= n; i++)
for (int l = W; l >= w[i]; l--) f[l] = max(f[l], f[l - w[i]] + v[i]);
2.完全背包(已优化)
// C++ Version
for (int i = 1; i <= n; i++)
for (int l = 0; l <= W - w[i]; l++)
f[l + w[i]] = max(f[l] + v[i], f[l + w[i]]);
// 由 f[i][l + w[i]] = max(max(f[i - 1][l + w[i]],f[i - 1][l] + w[i]),f[i][l +
// w[i]]); 简化而来
求dp数组中大于大于0的数量
ans = count_if(dp+1,dp+1+m,bind2nd(greater_equal<int>(),0));
3.多重背包
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[110], c[110],n,m;
int dp[100010];
int main(){
while(scanf("%d %d",&n,&m)&&n){
memset(dp,-1,sizeof(dp));
for(int i = 0; i < n; i ++)scanf("%d",&a[i]);
for(int i = 0; i < n; i ++)scanf("%d",&c[i]);
dp[0]=0;
for(int i = 0; i < n; i ++){
for(int j = 0; j <= m; j ++){
if(dp[j] >= 0)dp[j] = c[i];
else if(j < a[i] || dp[j-a[i]] <=0)dp[j] = -1;
else dp[j] = dp[j-a[i]]-1;
}
}
int ans = count_if(dp+1,dp+1+m,bind2nd(greater_equal<int>(),0));
printf("%d\n",ans);
}
return 0;
}
4.二进制分组优化
// C++ Version
index = 0;
for (int i = 1; i <= m; i++) {
int c = 1, p, h, k;
cin >> p >> h >> k;
while (k - c > 0) {
k -= c;
list[++index].w = c * p;
list[index].v = c * h;
c *= 2;
}
list[++index].w = p * k;
list[index].v = h * k;
}
同上一题代码!!!!优化的
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define Maxn 100010
#define INF 0x3f3f3f3f
int dp[Maxn],v[105],num[105],tmp_v[Maxn],book[Maxn];
int main(void)
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int kind,max_p;
while (scanf("%d%d",&kind,&max_p) != EOF) {
if(!kind && !max_p) break;
for (int i = 1; i <= kind; ++i) scanf("%d",&v[i]);
for (int i = 1; i <= kind; ++i) scanf("%d",&num[i]);
memset(dp,0,sizeof(dp));
int cnt = 1; // 用单独的数组存取拆分后的所有面值
for (int i = 1; i <= kind; ++i) {
for (int j = 1; j <= num[i]; j <<= 1) {
tmp_v[cnt] = j*v[i];
num[i]-=j;
cnt++;
}
if(num[i] > 0) tmp_v[cnt++] = num[i]*v[i];
}
//用上面拆分后的数组做01背包的处理
// 这里并没有用01背包去求最大值,而是把能够满足条件的状态dp 赋值为1,下标就是能够组合得到的总面值,
//否则为 0 ,不存在这样的面值
memset(book,0,sizeof(book));
int max_ = 1;
int ans = 0;
for (int i = 1; i < cnt; ++i) {
max_+=tmp_v[i];
max_ = min(max_p,max_);
for (int j = max_; j >= tmp_v[i]; --j) { //j的循环做了一些剪枝,因为遍历是从右往左的,在纸上模拟一下
if(dp[j]) continue; //就会明白,当前j遍历的最大值就是把以前用过的面值的累加,最小值就是
if(j == tmp_v[i]) dp[j] = 1; //当前物体的面值;
else if(j-tmp_v[i] > 0 && dp[j-tmp_v[i]]) dp[j] = 1;
if(!book[j] && dp[j]) { ans++; book[j] = 1; }
}
}
printf("%d\n",ans);
}
return 0;
}
5.分组背包
有 N 组物品和一个容量是 V的背包。
每组物品有若干个,同一组内的物品最多只能选一个。
每件物品的体积是 vij,价值是 wij,其中 i是组号,j是组内编号。求解将哪些物品装入背包,可使物品总体积不超过背包容量,且总价值最大。
输出最大价值。
它和01背包很像,只不过是分开成为组合!
动态规划题目:
1.01背包:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define endl '\n'
#define int long long
const int N = 2e5 + 5;
int dp[N];
int v[N], w[N];
void Solve() {
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 = m; j >= v[i]; --j)
dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
}
cout << dp[m] << endl;
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
Solve();
}
2.完全背包
void Solve() {
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 = v[i]; j <=m; ++j)
{
dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
}
}
cout << dp[m] << endl;
}
3.多重背包
int dp[N];
int v[N], w[N];
void Solve() {
int n, m; cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
int v, w, s;
cin >> v >> w >> s;
for (int j = m; j >= 0; --j)
{
for (int k = 0; k <= s && k * v <= j; ++k)
{
dp[j] = max(dp[j], dp[j - v * k] + w * k);
}
}
}
cout << dp[m] << endl;
}
4.多重背包二进制优化:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define endl '\n'
#define int long long
const int N = 2e5 + 5;
int dp[N];
int v[N], w[N];
int cnt[N];
void Solve() {
int cnt = 0;
int n, m; cin >> n >> m;
cnt = 0;
for (int i = 1; i <= n; ++i)
{
int vv, ww, ss; cin >> vv >> ww >> ss;
int k = 1;
while (k <= ss)
{
cnt++;
v[cnt] = vv * k;
w[cnt] = ww * k;
ss -= k;
k *= 2;
}
if (ss > 0)
{
cnt++;
v[cnt] = vv * ss;
w[cnt] = ww * ss;
}
}
n = cnt;
for (int i = 1; i <= n; ++i)
{
for (int j = m; j >= v[i];--j)
{
dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
}
}
cout << dp[m] << endl;
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
Solve();
}
5.分组背包
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define endl '\n'
#define int long long
const int N = 2e5 + 5;
int dp[N];
int v[N], w[N];
int cnt[N];
void Solve() {
int cnt;
int n, m; cin >> n >> m;
for (int k = 0; k < n; ++k)
{
int s; cin >> s;
cnt = 0;
for (int i = 1; i <= s; ++i)cin >> v[i] >> w[i];
for (int i =m; i >=0; --i)
{
for(int j=1;j<=s;++j)
if (i - v[j] >= 0)
dp[i] = max(dp[i], dp[i - v[j]] + w[j]);
}
}
cout << dp[m]<< endl;
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
Solve();
}