背包问题类型各题总结-acm

更新时间:2023-10-12 13:42:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

分组背包:

Problem C: 超市购物

Time Limit: 1 Sec Memory Limit: 128 MB

SUBMIT: 21 Solved: 10 [SUBMIT][STATUS]

Description

上次去超市扫荡回来的东西用完了,Staginner又得跑超市一趟,出发前他列了一张购物清单,打算去买K种不同的商品,每种买一件。到了超市,Staginner发现每种商品有N个品牌,每个品牌此商品的价格为Vi,对Staginner的作用值为Wi,他会从这N个品牌里面挑一个品牌买。这时,Staginner突然想起出门时只带了M元钱,又懒得去取钱了,所以不一定能买完K种商品,只好尽可能地让买的东西对自己的总作用值ans最大。

Input

多组样例。

第一行两个整数K,M代表Staginner想买的不同种类商品的数目和他带的钱 (0 < K <= 30, 0 < M <= 2000)

以下输入分为K个部分,代表K种商品。

每个部分第一行为一个数字N,代表第k种商品的N个品牌,N不大于10。之后跟着N行,每行两个数字,代表物品的价格Vi和作用值Wi。其中 0 < Vi < M。

Output

输出Case #: 最大总作用值,每两个样例之间有一个空行。

Sample Input

3 100 3

50 600 20 700 30 800 2

30 500 40 600

1

60 200 2 500 2

200 1000 260 1200 1

280 300

Sample Output

Case 1: 1400 Case 2: 1300

HINT

#include #include

int bag[100][100],dp[2500],val[100][100]; int main() {

int k,m,n,i,j,t,cnt=0;

while(scanf(\ {

memset(dp,0,sizeof(dp)); memset(val,0,sizeof(val)); memset(bag,0,sizeof(bag)); for(i=0;i

scanf(\ bag[i][0]=n;

for(j=1;j<=n;j++)

scanf(\ }

for(i=0;i=0;j--)

for(k=1;k<=bag[i][0];++k) if(bag[i][k]<=j)

dp[j]=dp[j]>(dp[j-bag[i][k]]+val[i][k])?dp[j]:(dp[j-bag[i][k]]+val[i][k]);

printf(\

printf(\ }

return 0; }

Robberies

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3802 Accepted Submission(s): 1433

Problem Description

The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university.

For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.

His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.

Input

The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj .

Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .

Output

For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.

Notes and Constraints 0 < T <= 100 0.0 <= P <= 1.0 0 < N <= 100

0 < Mj <= 100 0.0 <= Pj <= 1.0

A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.

Sample Input 3

0.04 3 1 0.02 2 0.03 3 0.05 0.06 3 2 0.03 2 0.03 3 0.05 0.10 3 1 0.03 2 0.02 3 0.05

Sample Output

2 4 6

题意:输入一个数 表示一共多少组数据 输入一个浮点型数表示小偷被抓的几率不超过这个情况下能偷的最多钱 输入一个整形表示多少个银行 输入3组数据 一组2个数 分别是该银行的钱 以及被抓的几率 求小偷不被抓的情况下能偷的最多钱

思路: 一开始我是让几率当背包 然后钱做val 但是 这样是错误的 因为几率是浮点型 虽然可以*100变成整形 但是 几率是相乘得出的 而几率当背包无法模拟几率的相乘 由题中数据看 仿佛用几率当背包 然后几率相加 能得到样例中的结果 但是这是错误的思想 几率哪有加的啊

所以就要变通一下 把钱当背包 看总共多少钱 把几率当背包

#include #include double val[105],x; int wei[105];

double bag[10500000]; void _01bag(int v,int m) {

int i,j;

for(i=0;i

for(i=0;i

for(j=v;j>=wei[i];j--)

if(bag[j]=0;i--)

if(bag[i]>=1-x) {printf(\ return; }

int main() {

int t,i,m,v;

scanf(\ while(t--) {

v=0;

scanf(\ for(i=0;i

scanf(\ v=v+wei[i]; }

_01bag(v,m); }

return 0; }

I NEED A OFFER!

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8472 Accepted Submission(s): 3083

Problem Description

Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要

上述方法在保存状态F[][]及G[][]时需要O(NV)的空间复杂度,下面我们对空间复制度进行优化。

压缩空间复杂度为O(V)

F[i][j]与G[i][j]只分别与F[i-1][]和G[i-1][]的状态有关,所以我们可以用两个一维数组F[]和G[]来替换二维数组F[][]和G[][]。具体思想请看博文

<<背包问题——“01背包”详解及实现(包含背包中具体物品的求解)>>

下面直接给出伪代码: [cpp] view plaincopyprint?

1. F[] ← 0 2.

3. G[] ← 1 4.

5. for i ← 1 to N 6.

7. do for j ← V to C[i] 8.

9. if (F[j] < F[j-C[i]]+W[i]) 10.

11. then F[j] ← F[j-C[i]]+W[i] 12.

13. G[j] ← G[j-C[i]] 14.

15. else if (F[j] = F[j-C[i]]+W[i]) 16.

17. then G[j] ← G[j]+G[j-C[i]] 18.

19. return F[V] and G[V]

F[] ← 0 G[] ← 1 for i ← 1 to N do for j ← V to C[i] if (F[j] < F[j-C[i]]+W[i]) then F[j] ← F[j-C[i]]+W[i] G[j] ← G[j-C[i]] else if (F[j] = F[j-C[i]]+W[i]) then G[j] ← G[j]+G[j-C[i]] return F[V] and G[V]

分组背包 ACboy needs your help

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1127 Accepted Submission(s): 575

Problem Description

ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the profit?

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.

Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j]. N = 0 and M = 0 ends the input.

Output

For each data set, your program should output a line which contains the number of the max profit ACboy will gain.

Sample Input

2 21 21 32 22 12 12 33 2 13 2 10 0

Sample Output

346

http://acm.hdu.edu.cn/showproblem.php?pid=1712

这是一个分组背包问题

这种题目的特点是 物品被划分为若干组,每组中的物品互相冲突,只能从中选取一个或者不选

这个问题变成了每组物品有若干种策略:是选择本组的某一件,还是一件都不选。也就是说设f[k][v]表示前k组物品花费费用v能取得的最大权值,

则有f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]|物品i属于第k组}。 我的代码:

#include #include int va[105][105];

int dp[105]; int main() {

int sum_course,sum_day,i,j,k;

while(scanf(\ {

if(sum_course==0&&sum_day==0) return 0; k=0;

for(i=1;i<=sum_course;i++) for(j=1;j<=sum_day;j++)

scanf(\ memset(dp,0,sizeof(dp));

for(k=1;k<=sum_course;k++)//所有的组数 此题中即前坐标 for(j=sum_day;j>=0;j--)//这个控制背包的容积

for(i=1;i<=j;i++)//当前组的所有成员 对应后坐标 这句话不能与上面的交换

dp[j]=dp[j]>dp[j-i]+va[k][i]?dp[j]:dp[j-i]+va[k][i];

/*上面那句话的意识是 如果对于某一组的中的某一个元素 或者说物品 看有它和没它的时候哪个更大些 取大的*/

printf(\ }

return 0; }

网上复制简洁代码: #include #include using namespace std; const int mm=111; int f[mm],a[mm][mm]; int i,j,k,n,m; int main() {

while(scanf(\ {

for(i=0;i<=m;++i)f[i]=0; for(i=0;i

for(j=1;j<=m;++j)scanf(\ for(i=0;i=0;--j) for(k=1;k<=j;++k)

f[j]=max(f[j],f[j-k]+a[i][k]); printf(\ }

return 0; }

下面是我用二维数组做的 直接超时 #include #include struct haha {

int val;

int use_day; }va[10005];

int dp[105][105]; int main() {

int sum_course,sum_day,i,j,k;

while(scanf(\ {

if(sum_course==0&&sum_day==0) return 0; k=0;

for(i=1;i<=sum_course;i++) for(j=1;j<=sum_day;j++) {

scanf(\ va[k-1].use_day=j; }

memset(dp,0,(sum_course*sum_day+10)*sizeof(dp[0][0]));

for(k=0;k=1;i--)

for(j=sum_day;j>=va[k].use_day;j--)

dp[i][j]=dp[i][j]>dp[i-1][j-va[k].use_day]+va[k].val?dp[i][j]:dp[i-1][j-va[k].use_day]+va[k].val;

printf(\ }

return 0; }

二维背包之杭电3496Watch The Movie

二维背包其实就是相当 与2个背包 只不过是多了一个循环

下面的程序设计当了背包的恰好问题 很值得看看

Watch The Movie

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2344 Accepted Submission(s): 764

Problem Description

New semester is coming, and DuoDuo has to go to school tomorrow. She decides to have fun tonight and will be very busy after tonight. She like watch cartoon very much. So she wants her uncle to buy some movies and watch with her tonight. Her grandfather gave them L minutes to watch the cartoon. After that they have to go to sleep.

DuoDuo list N piece of movies from 1 to N. All of them are her favorite, and she wants her uncle buy for her. She give a value Vi (Vi > 0) of the N piece of movies. The higher value a movie gets shows that DuoDuo likes it more. Each movie has a time Ti to play over. If a movie DuoDuo choice to watch she won?t stop until it goes to end.

But there is a strange problem, the shop just sell M piece of movies (not less or more then), It is difficult for her uncle to make the decision. How to select M piece of movies from N piece of DVDs that DuoDuo want to get the highest value and the time they cost not more then L. How clever you are! Please help DuoDuo?s uncle.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case:

The first line is: N(N <= 100),M(M<=N),L(L <= 1000) N: the number of DVD that DuoDuo want buy. M: the number of DVD that the shop can sale.

L: the longest time that her grandfather allowed to watch.

The second line to N+1 line, each line contain two numbers. The first number is the time of the ith DVD, and the second number is the value of ith DVD that DuoDuo rated.

Output

Contain one number. (It is less then 2^31.) The total value that DuoDuo can get tonight.

获得的最大价值。则其状态转移方程便是:

f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]} 这个方程非常重要,基本上所有跟背包相关的问题的方程都是由它衍生出来的。所以有必要将它详细解释一下:“将前i件物品放入容量为v的背包中”这个子问题,若只考虑第i件物品的策略(放或不放),那么就可以转化为一个只牵扯前i-1件物品的问题。如果不放第i件物品,那么问题就转化为“前i-1件物品放入容量为v的背包中”,价值为

f[i-1][v];如果放第i件物品,那么问题就转化为“前i-1件物品放入剩下的容量为v-c[i]的背包中”,此时能获得的最大价值就是f[i-1][v-c[i]]再加上通过放入第i件物品获得的价值w[i]。

这时候我们可以用二维数组进行做了

//二维数组实现背包 #include

#include #define N 1002 int max[N][N]; int main() {

int jiazhi[N],tiji[N],t,i,j,n,v; scanf(\ while(t--) {

scanf(\ for(i=1;i<=n;i++)

scanf(\ for(i=1;i<=n;i++) scanf(\

memset(max,0,sizeof(max)); for(i=1;i<=n;i++) for(j=0;j<=v;j++) {

if(tiji[i]<=j&&max[i-1][j]

max[i][j]=max[i-1][j]; }

printf(\ }

return 0; }

但是我们为了以后解决更加复杂的背包 必须学会用一维数组解决它

先考虑上面讲的基本思路如何实现,肯定是有一个主循环i=1..N,每次算出来二维数组f[i][0..V]的所有值。那么,如果只用一个数组f[0..V],能不能保证第i次循环结束后f[v]中表示的就是我们定义的状态f[i][v]呢?f[i][v]是由f[i-1][v]和f[i-1][v-c[i]]两个子问题递推而来,能否保证在推f[i][v]时(也即在第i次主循环中推f[v]时)能够得到f[i-1][v]和f[i-1][v-c[i]]的值呢?事实上,这要求在每次主循环中我们以v=V..0的顺序推f[v],这样才能保证推f[v]时f[v-c[i]]保存的是状态f[i-1][v-c[i]]的值。如果将v的循环顺序从上面的逆序改成顺序的话,那么则成了f[i][v]

由f[i][v-c[i]]推知,与本题意不符,但它却是另一个重要的背包问题P02最简捷的解决方案,故学习只用一维数组解01背包问题是十分必要的。

伪代码如下: for i=1..N for v=V..0

f[v]=max{f[v],f[v-c[i]]+w[i]};

画个图给大家演示下

也就是说此时的f[v],f[v-c[i]] 是前面的

假设体积是10

背包体积----->>>>> 价大1 2 3 值 小 2 1 2 2 2 2 1 2 2 2 3 5 3 2 2 3 3 4 5 6 7 8 9 10 2 2 2 2 2 2 3 3 3 3 3 3 3 8 8 8 8 8 3 8 8 8 8 12 放 第 一 个 物 品 放 第 二 个 3 4 4 2 2 3 3 3 5 可以看出 f[v]=max{f[v],f[v-c[i]]+w[i]}; V是从大到小的 其中的f[v],f[v-c[i]]是前面的值 比较的结果赋值给f[v] 比如说当i=5的时候 f[v]>f[v-5]注意两者都是i=4时候的值得出的结果12 赋值到新的f[v] 但是此时的f[v]是i=5的 当把最后一行改写成55 6的时候 我们可以看出 f[v-6]=f[4]+55 大于f[v] 所以我们可以得出新的f[v]=f[4]+55这样结果就是取的体积为1 2 6 代码如下//一维数组实现背包问题 #include #include #define N 1002 int main() {

int dp[N],vol[N],val[N]; int t,n,i,j,v;

scanf(\ while(t--) {

scanf(\ for(i=1;i<=n;i++)

scanf(\ for(i=1;i<=n;i++)

scanf(\ memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++)

for(j=v;j>=vol[i];j--)

if(dp[j]

return 0; }

初始化的细节问题

我们看到的求最优解的背包问题题目中,事实上有两种不太相同的问法。有的题目要求“恰好装满背包”时的最优解,有的题目则并没有要求必须把背包装满。一种区别这两种问法的实现方法是在初始化的时候有所不同。

如果是第一种问法,要求恰好装满背包,那么在初始化时除了f[0]为0其它f[1..V]均设为-∞,这样就可以保证最终得到的f[N]是一种恰好装满背包的最优解。

如果并没有要求必须把背包装满,而是只希望价格尽量大,初始化时应该将f[0..V]全部设为0。 为什么呢?可以这样理解:初始化的f数组事实上就是在没有任何物品可以放入背包时的合法状态。如果要求背包恰好装满,那么此时只有容量为0的背包可能被价值为0的nothing“恰好装满”,其它容量的背包均没有合法的解,属于未定义的状态,它们的值就都应该是-∞了。如果背包并非必须被装满,那么任何容量的背包都有一个合法解“什么都不装”,这个解的价值为0,所以初始时状态的值也就全部为0了。

这个小技巧完全可以推广到其它类型的背包问题,后面也就不再对进行状态转移之前的初始化进行讲解

01背包”及“完全背包”装满背包的方案总数分析及实现

本文为网上复制

本人博文《背包问题——“01背包”最优方案总数分析及实现》及《背包问题——“完全背包”最优方案总数分析及实现》中分别谈过“01背包”和“完全背包”实现最大价值的方案总数,这里我们再讨论一下这两种背包被物品刚好装满的方案总数。

本文来源:https://www.bwwdw.com/article/pmqf.html

Top