背包问题

动态规划中运用非常多的是背包问题

背包问题一共有三种:

1. 0-1背包问题

2. 完全背包问题(需要把背包装满,只需要在0-1背包的基础上改变一下初始化即可)

3. 多重背包问题(相当于0-1背包问题的演变)

例一 九度1123 0-1背包问题

题目描述:

辰辰是个很有潜能、天资聪颖的孩子,他的梦想是称为世界上最伟大的医师。
为此,他想拜附近最有威望的医师为师。医师为了判断他的资质,给他出了一个难题。
医师把他带到个到处都是草药的山洞里对他说:
“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株也有它自身的价值。
我会给你一段时间,在这段时间里,你可以采到一些草药。如果你是一个聪明的孩子,你应该可以让采到的草药的总价值最大。”
如果你是辰辰,你能完成这个任务吗?

输入:

输入的第一行有两个整数T(1 <= T <= 1000)和M(1 <= M <= 100),T代表总共能够用来采药的时间,M代表山洞里的草药的数目。
接下来的M行每行包括两个在1到100之间(包括1和100)的的整数,分别表示采摘某株草药的时间和这株草药的价值。

输出:

可能有多组测试数据,对于每组数据,
输出只包括一行,这一行只包含一个整数,表示在规定的时间内,可以采到的草药的最大总价值。 

样例输入:
70 3
71 100
69 1
1 2
样例输出:
3

#include<stdio.h>
using namespace std;
struct caoyao {
	int weight;
	int time;
}cy[101];
int dp[1001]; //表示每个时间所对应的价值状态
int max(int a, int b) { return a > b ? a : b; }
int main() {
	int T, M;
	while (scanf("%d%d", &T, &M) != EOF) {  //T表示总时间 M表示草药的数目
		for (int i = 1; i <= M; i++) {
			scanf("%d%d", &cy[i].time, &cy[i].weight);
		}
		for (int i = 0; i <= T; i++) {
			dp[i] = 0;
		}
		for (int i = 1; i <= M; i++) {
			for (int j = T; j >= cy[i].time; j--) {
				dp[j] = max(dp[j], dp[j - cy[i].time] + cy[i].weight);
			}
		}
		printf("%d\n", dp[T]);
	}
}

例二  利用二维数组表示,同时需要输出具体需要哪几项

#include<stdio.h>
using namespace std;
struct caoyao {
	int weight;
	int time;
}cy[101];
int x[101];
int T, M;
int dp[101][1001]; //表示每个时间内相应草药数目所对应的价值状态
int max(int a, int b) { return a > b ? a : b; }

//用来判断选择哪几样
void traceback() {
	for (int i = M; i > 1; i--) {
		if (dp[i][T] == dp[i - 1][T]) {
			x[i] = 0;
		}
		else {
			x[i] = 1;
			T -= cy[i].time;
		}
	}
	if (dp[1][T] > 0) { x[1] = 1; }
	else {
		x[1] = 0;
	}
}

int main() {
	while (scanf("%d%d", &T, &M) != EOF) {
		for (int i = 1; i <= M; i++) {
			scanf("%d%d", &cy[i].time, &cy[i].weight);
		}
		for (int i = 0; i <= T; i++) {
			dp[0][i] = 0;
		}
		for (int i = 1; i <= M; i++) {
			for (int j = 1; j <= T; j++) {
				if (j >= cy[i].time) {
					dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cy[i].time] + cy[i].weight);
				}
				else {
					dp[i][j] = dp[i - 1][j];
				}
			}
		}
		printf("%d\n", dp[M][T]);
		traceback();
		for (int i = 1; i <= M; i++) {
			if (x[i] == 1) { printf("%d ", i); }
		}
	}
}

例二 九度1454(完全背包问题)

题目描述:

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid. 
But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!

输入:

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams.

输出:

Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".

样例输入:
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
样例输出:
The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible. 
#include<stdio.h>
using namespace std;
#define INF 0x7fffffff
struct C {
	int p; //代表价值
	int w; //代表重量
};
int min(int a, int b) { return a < b ? a : b; }
C coin[501];
int dp[10001];
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int E, F;
		scanf("%d%d", &E, &F);
		int Mw;
		Mw = F - E;
		int N;
		scanf("%d", &N);
		int i, j;
		for (i = 1; i <= N; i++) {
			scanf("%d%d", &coin[i].p, &coin[i].w);
		}
		for (i = 0; i <= Mw; i++) {
			dp[i] = INF;
		}
		dp[0] = 0; //对于完全背包来说,只需要初始化dp[0],其他的都赋为无穷(不存在)
		for (i = 1; i <= N; i++) {
			for (j = coin[i].w; j <= Mw; j++) {
				if (dp[j-coin[i].w] != INF) {  //只有前一个状态不为无穷,才能由那个状态转移过来
					dp[j] = min(dp[j], dp[j - coin[i].w] + coin[i].p);
				}
			}
		}
		if (dp[Mw] != INF) {
			printf("The minimum amount of money in the piggy-bank is %d.\n",dp[Mw]);
		}
		else {
			printf("This is impossible.\n");
		}
	}
}

例三 九度1455(多重背包问题)

其实就是将原数量为k的物品拆分成若干组,拆分方式类似于二进制拆分,从而可以表示所有的数
题目描述:

为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。请问:你用有限的资金最多能采购多少公斤粮食呢?

输入:

输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1&lt;=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。

输出:

对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。

样例输入:
1
8 2
2 100 4
4 100 2
样例输出:
400
#include<stdio.h>
using namespace std;
struct E {
	int p; //代表每袋的价格
	int h; //代表每袋的重量
};
int max(int a, int b) { return a > b ? a : b; }
E rice[20001];
int dp[201];
int main() {
	int C;
	scanf("%d", &C);
	while (C--) {
		int n, m;
		scanf("%d%d", &n, &m); //表示经费和大米的种类
		int p, h, c; //表示每袋价格、重量、袋数
		int i, j, k;
		int cnt = 0;
		for (i = 1; i <= m; i++) {
			scanf("%d%d%d", &p, &h, &k);
			int c = 1;
			while (k - c > 0) {
				k -= c;
				cnt++;
				rice[cnt].h = c*h;
				rice[cnt].p = c*p;
				c *= 2;
			}
			cnt++;
			rice[cnt].h = k*h;
			rice[cnt].p = k*p;
		}
		for (i = 0; i <= n; i++) {
			dp[i] = 0;
		}
		for (i = 1; i <= cnt; i++) {
			for (j = n; j >=rice[i].p; j--) {
				dp[j] = max(dp[j], dp[j - rice[i].p] + rice[i].h);
			}
		}
		printf("%d\n", dp[n]);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值