背包问题


01背包

模板题目

传送门

code
#include <bits/stdc++.h>
const int maxn = 2e3 + 100;
typedef long long LL;

template <typename T>
inline void read(T &s) {
    s = 0;
    T w = 1, ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch))  { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    s *= w;
}

int n;
int m;
int w[maxn];
int v[maxn];
int f[maxn];

int main() {
    read(n), read(m);
    for (int i = 1; i <= n; ++i) read(w[i]), read(v[i]);
    
    // std::memset(f, 0xcf, sizeof(f));
    f[0] = 0;
    
    for (int i = 1; i <= n; ++i) {
        for (int j = m; j >= w[i]; --j) {
            f[j] = std::max(f[j], f[j - w[i]] + v[i]);
        }
    }
    printf("%d\n", f[m]);
    return 0;
}


完全背包

模板题目

传送门

code
#include <bits/stdc++.h>
const int maxn = 5e5 + 100; 

template <typename T> 
inline void read(T &s) {
    s = 0; 
    T w = 1, ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch))  { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    s *= w;
}

int n;
int m;
int w[maxn];
int v[maxn];
int f[maxn];

int main() {
    read(n), read(m);
    for (int i = 1; i <= n; ++i) read(w[i]), read(v[i]);
    
    f[0] = 0;
    
    for (int i = 1; i <= n; ++i) {
        for (int j = w[i]; j <= m; ++j) {
            f[j] = std::max(f[j], f[j - w[i]] + v[i]);
        }
    }
    
    printf("%d\n", f[m]);
    return 0;
}

多重背包

模板题目

传送门

普通板本code
#include <bits/stdc++.h>
const int maxn = 150;

template <typename T>
inline void read(T &s) {
    s = 0;
    T w = 1, ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch))  { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    s *= w;
}

int n;
int m;
int s[maxn];
int v[maxn];
int w[maxn];
int f[maxn];

int main() {
    read(n), read(m);
    for (int i = 1; i <= n; ++i) {
        read(w[i]);
        read(v[i]);
        read(s[i]);
    }
    
    f[0] = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = m; j >= w[i]; --j) {
            for (int k = 1; k <= s[i] && k * w[i] <= j; ++k) {
                f[j] = std::max(f[j], f[j - k * w[i]] + k * v[i]);
                // printf()
            } 
        }
    }
    
    printf("%d\n", f[m]);
    return 0;
}
2进制优化code
#include <bits/stdc++.h>
const int maxn = 1e4 + 200;
typedef long long LL;

template <typename T> 
inline void read(T &s) {
    s = 0;
    T w = 1, ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch))  { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    s *= w;
}

int n;
int m;
int size;
LL w[maxn];
LL v[maxn];
LL s[maxn];
LL r[maxn];
LL f[maxn];

int main() {
    read(n), read(m);
    for (int i = 1; i <= n; ++i) {
        LL x, y, z, t = 1;
        read(x); //w 
        read(y); //v
        read(z); //n
        while (t <= z) {
            w[++size] = x * t;
            v[size] = y * t;
            z -= t;
            t <<= 1;
        }
        // printf("%d == \n", t);
        w[++size] = x * z;
        v[size] = y * z;
    }
    
    f[0] = 0;
    for (int i = 1; i <= size; ++i) {
        for (int j = m; j >= w[i]; --j) {
            f[j] = std::max(f[j], f[j - w[i]] + v[i]);
        }
    }
    
    printf("%lld\n", f[m]);
    return 0;
}

例题:逃亡的准备(小数据 — 普通多重背包)

描述 Description
在《Harry Potter and the Deathly Hallows》中,Harry Potter他们一起逃亡,现在有许多的东西要放到赫敏的包里面,但是包的大小有限,所以我们只能够在里面放入非常重要的物品,现在给出该种物品的数量、体积、价值的数值,希望你能够算出怎样能使背包的价值最大的组合方式,并且输出这个数值,赫敏会非常地感谢你。
输入格式 Input Format
(1)第一行有2个整数,物品种数n和背包装载体积v。
(2)2行到n+1行每行3个整数,为第i种物品的数量m、体积w、价值s。.
输出格式 Output Format
仅包含一个整数,即为能拿到的最大的物品价值总和。
样例输入 Sample Input
2 10
3 4 3
2 2 5
样例输出 Sample Output
13
【注释】
选第一种一个,第二种两个。
结果为31+52=13

code
#include <bits/stdc++.h>

using namespace std;

#define MAXX 20500

int n, t;
int num[MAXX], w[MAXX], v[MAXX], f[MAXX];
int sum = 0;

inline int read()
{
	int s = 0, w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') 
	{
		if(ch == '-') w = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		s = (s<<1) + (s<<3) + ch - '0';
		ch = getchar();
	}
	return s * w;
}

int main()
{
//	freopen("kl.in","r",stdin);
//	freopen("kl.out","w",stdout);
	memset(num, 0, sizeof(num));
	memset(w, 0, sizeof(w));
	memset(v, 0, sizeof(v));
	memset(f, 0, sizeof(f));
	n = read();
	t = read();
	for(int i = 1; i <= n; ++i) 
	{
		num[i] = read();
		w[i] = read();
		v[i] = read();
	}
	for(int i = 1; i <= n; ++i)
	{
		if(num[i] * w[i] >= t)
		{
			for(int j = w[i]; j <= t; ++j)
			{
				f[j] = max(f[j - w[i]] + v[i], f[j]);
			}
		}
		else 
		{
			for(int z = 1; z <= num[i]; z++)
			{
				for(int j = t; j >= w[i]; j--)
				{
					f[j] = max(f[j - w[i]] + v[i], f[j]);
				}
			}
		}
	}
	printf("%d", f[t]);
	return 0;
} 

例题:逃亡的准备(大数据版 – 2进制优化多重背包)

描述 Description
在《Harry Potter and the Deathly Hallows》中,Harry Potter他们一起逃亡,现在有许多的东西要放到赫敏的包里面,但是包的大小有限,所以我们只能够在里面放入非常重要的物品,现在给出该种物品的数量、体积、价值的数值,希望你能够算出怎样能使背包的价值最大的组合方式,并且输出这个数值,赫敏会非常地感谢你。
输入格式 Input Format
(1)第一行有2个整数,物品种数n和背包装载体积v。
(2)2行到n+1行每行3个整数,为第i种物品的数量m、体积w、价值s。.
输出格式 Output Format
仅包含一个整数,即为能拿到的最大的物品价值总和。
样li输入 Sample Input
2 10
3 4 3
2 2 5
样例输出 Sample Output
13
【注释】
选第一种一个,第二种两个。
结果为31+52=13
时间限制 Time Limitation
1s
注释 Hint
对于100%的数据
1<=v<=5000
1<=n<=5000
1<=m<=5000
1<=w<=5000
1<=s<=5000

code
#include <bits/stdc++.h>

using namespace std;

#define MAXX 20500

int n, t;
int num[MAXX], w[MAXX], v[MAXX], f[MAXX];
int sum = 0;

inline int read()
{
	int s = 0, w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') 
	{
		if(ch == '-') w = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		s = (s<<1) + (s<<3) + ch - '0';
		ch = getchar();
	}
	return s * w;
}

int main()
{
//	freopen("kl.in","r",stdin);
//	freopen("kl.out","w",stdout);
	memset(num, 0, sizeof(num));
	memset(w, 0, sizeof(w));
	memset(v, 0, sizeof(v));
	memset(f, 0, sizeof(f));
	n = read();
	t = read();
	int two = 1;
	for(int i = 1; i <= n; ++i) 
	{
		num[i] = read();
		w[i] = read();
		v[i] = read();
	}
	for(int i = 1; i <= n; ++i)
	{
		if(num[i] * w[i] >= t)
		{
			for(int j = w[i]; j <= t; ++j)
			{
				f[j] = max(f[j - w[i]] + v[i], f[j]);
			}
		}
		else 
		{
			for(int j = num[i]; j >= 0; j -= two)
			{
				for(int z = t; z >= w[i]; z--)
				{
					f[z] = max(f[z - w[i]] + v[i], f[z]);
				}
				two *= 2;
			}
		}
	}
	printf("%d", f[t]);
	return 0;
} 


混合背包

模板题目

传送门
题解:只需要转化为01,完全, 分组背包就可以

code
#include <bits/stdc++.h>
const int maxn = 5e5 + 100;
typedef long long LL;

template <typename T>
inline void read(T &s) {
    s = 0;
    T w = 1, ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch))  { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    s *= w;
}

int n;
int m;
int size;
int v[maxn];
int w[maxn];
int k[maxn];
int f[maxn];

int main() {
    size = 0;
    read(n), read(m);
    for (int i = 1, x, y, z, t; i <= n; ++i) {
        read(x), read(y), read(z);
        if (z < 0) { w[++size] = x, v[size] = y; k[size] = 1; }
        else if (z == 0) { w[++size] = x, v[size] = y; k[size] = 0; }
        else if (z > 0) {
            t = 1;
            while (t <= z) {
                w[++size] = x * t;
                v[size] = y * t;
                k[size] = 1;
                z -= t;
                t <<= 1;
            }
            w[++size] = x * z;
            v[size] = y * z;
            k[size] = 1;
        }
    }
    
    f[0] = 0;
    for (int i = 1; i <= size; ++i) {
        if (k[i])
            for (int j = m; j >= w[i]; --j)
                f[j] = std::max(f[j], f[j - w[i]] + v[i]);
        else
            for (int j = w[i]; j <= m; ++j)
                f[j] = std::max(f[j], f[j - w[i]] + v[i]);
    }
    
    printf("%d\n", f[m]);
    return 0;
}

二维费用的背包问题

模板题目

传送门

code
#include <bits/stdc++.h>
const int maxn = 2e3 + 10;

template <typename T>
inline void read(T &s) {
    s = 0;
    T w = 1, ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch))  { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    s *= w;
}

int n;
int av;
int am;
int v[maxn];
int w[maxn];
int val[maxn];
int f[maxn][maxn];

int main() {
    read(n), read(av), read(am);
    for (int i = 1; i <= n; ++i) {
        read(v[i]);
        read(w[i]);
        read(val[i]); 
    }
    
    f[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = av; j >= v[i]; --j) {
            for (int k = am; k >= w[i]; --k) {
                f[j][k] = std::max(f[j][k], f[j - v[i]][k - w[i]] + val[i]);
            }
        }
    }
    
    printf("%d\n", f[av][am]);
    return 0;
}

例题:郁闷的潜水员

描述 Description
潜水员为了潜水要使用特殊的装备。他有一个带2种气体的气缸:一个为氧气,一个为氮气。让潜水员下潜的深度需要各种的数量的氧和氮。潜水员有一定数量的气缸。每个气缸都有重量和气体容量。潜水员为了完成他的工作需要特定数量的氧和氮。他完成工作所需气缸的总重的最低限度的是多少?
例如:潜水员有5个气缸。每行三个数字为:氧,氮的(升)量和气缸的重量:
3 36 120
10 25 129
5 50 250
1 45 130
4 20 119
如果潜水员需要5升的氧和60升的氮则总重最小为249 (1,2或者4,5号气缸)。
你的任务就是计算潜水员为了完成他的工作需要的气缸的重量的最低值。
输入格式 Input Format
第一行有2整数t,a(1<=t<=21,1<=a<=79)。它们表示氧,氮各自需要的量。
第二行为整数n (1<=n<=1000)表示气缸的个数。
此后的n行,每行包括ti,ai,wi(1<=ti<=21,1<=ai<=79,1<=wi<=800)3整数。这些各自是:第i个气缸里的氧和氮的容量及汽缸重量。
输出格式 Output Format
一行包含一个整数,为潜水员完成工作所需的气缸的重量总和的最低值。
样例输入 Sample Input
5 60
5
3 36 120
10 25 129
5 50 250
1 45 130
4 20 119
样例输出 Sample Output
249

code
#include <bits/stdc++.h>
#define up(i, a, b) for(register int i = a; i <= b; ++i)
#define down(i, a, b) for(register int i = a; i >= b; i--) 
using namespace std;
const int MAXX = 1e3 + 10;
typedef long long ll;

inline int read() {
	int s = 0, w = 1;
	char ch = getchar();
	while (!isdigit(ch)) { if(ch == '-') w = -1; ch = getchar(); }
	while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
	return s * w;
}

int t, a;
int n;
int vt[MAXX], va[MAXX], w[MAXX];
int f[MAXX][MAXX];

int main() {
	memset(f, 15, sizeof(f));
	t = read(); a = read(); n = read();
	for (int i = 1; i <= n; ++i) {
		vt[i] = read(); va[i] = read(); w[i] = read();
	}
	for (int k = 1; k <= n; ++k) {
		for (int i = t; i >= 0; i--) {
			for (int j = a; j >= 0; j--) {
				if(vt[k] < i && va[k] < j) {
					f[i][j] = min(f[i - vt[k]][j - va[k]] + w[k], f[i][j]);
				}
				else if(vt[k] < i) {
					f[i][j] = min(f[i - vt[k]][j] + w[k], f[i][j]);
				}
				else if(va[k] < j) {
					f[i][j] = min(f[i][j - va[k]] + w[k], f[i][j]);
				}
				else {
					f[i][j] = min(w[k], f[i][j]);
				}
			}
		}
	}
	printf("%d\n", f[t][a]);
	return 0;
}

分组背包

感谢hsm,startd的帮助

模板题目

传送门

code

#include <bits/stdc++.h>
const int maxn = 105;
const int inf = 0x3f3f3f3f;
typedef long long LL;

template <typename T>
inline void read(T &s) {
    s = 0;
    T w = 1, ch = getchar(); 
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch))  { s = (s << 1) +(s << 3) + (ch ^ 48); ch = getchar(); }
    s *= w;
}

int N;
int V;
int s[maxn];
int w[maxn][maxn];
int v[maxn][maxn];
int f[maxn];

int main() {
    read(N), read(V);
    for (int i = 1; i <= N; ++i) {
        read(s[i]);
        for (int j = 1; j <= s[i]; ++j) {
            read(w[i][j]);
            read(v[i][j]);
        }
    }
    
    f[0] = 0;
    for (int i = 1; i <= N; ++i) {
        for (int j = V; j >= 0; --j) {
            for (int k = 1; k <= s[i]; ++k) {
                if (j >= w[i][k]) {
                    f[j] = std::max(f[j], f[j - w[i][k]] + v[i][k]);
                }
            }
        }
    }
    
    printf("%d\n", f[V]);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值