动态规划 -- 01背包问题和完全背包问题

Posted hi3254014978

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态规划 -- 01背包问题和完全背包问题相关的知识,希望对你有一定的参考价值。

动态规划的01背包问题和完全背包问题模板

01背包问题模板:

// 01背包问题
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 100;        // 物品的最大件数
const int maxv = 1000;        // V的上限

int w[maxn], c[maxn], dp[maxv];

int main()
{
    
    // 边界
    for (int v = 0; v <= V; v++){
        dp[v] = 0;
    }

    for (int i = 1; i <= n; i++){
        for (int v = V; v >= w[i]; v--){        // 逆向枚举v
            // 状态转移方程
            dp[v] = max(dp[v], dp[v - w[i]] + c[i]);
        }
    }

    // 寻找dp[0] ... dp[V]中的最大值即为答案
    int max = 0;
    for (int v = 0; v <= V; v++){
        if (dp[v] > max){
            max = dp[v];
        }
    }
}

 

完全背包问题模板:

for (int i = 1; i <= n; i++){
    for (int v = w[i]; v <= V; v++){
        // 状态转移方程
        dp[v] = max(dp[v], dp[v - w[i]] + c[i]);
    }
}

 

01背包问题实战:

              1068 Find More Coins (30分)

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 10?4?? coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (10?4??, the total number of coins) and M (10?2??, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the face values V?1??V?2???V?k?? such that V?1??+V?2??+?+V?k??=M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output "No Solution" instead.

Note: sequence {A[1], A[2], ...} is said to be "smaller" than sequence {B[1], B[2], ...} if there exists k1 such that A[i]=B[i] for all i<k, and A[k] < B[k].

Sample Input 1:

8 9
5 9 8 7 2 3 4 1

Sample Output 1:

1 3 5

Sample Input 2:

4 8
7 2 4 3

Sample Output 2:

No Solution

分析:这题的价值和容量数组是同一个数组,但是还需要记录下路径,所以多了一个choice[][]数组

完整代码:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int maxn = 10010;
 6 const int maxv = 110;
 7 
 8 int w[maxn], dp[maxv] = { 0 };        // w[i]为钱币的价值
 9 bool choice[maxn][maxv], flag[maxn];
10 bool cmp(int a, int b){                    // 从大到小排序
11     return a > b;
12 }
13 
14 int main()
15 {
16     // freopen("in.txt", "r", stdin);
17     int n, m;
18     scanf("%d %d", &n, &m);
19     for (int i = 1; i <= n; i++){
20         scanf("%d", &w[i]);
21     }
22     
23     sort(w + 1, w + n + 1, cmp);        // 从大到小排序
24     for (int i = 1; i <= n; i++){
25         for (int v = m; v >= w[i]; v--){
26             // 状态转移方程
27             if (dp[v] <= dp[v - w[i]] + w[i]){
28                 dp[v] = dp[v - w[i]] + w[i];
29                 choice[i][v] = 1;                // 放入第i 件物品
30             }
31             else{
32                 choice[i][v] = 0;            // 不放入第i 件物品
33             }
34         }
35     }
36     if (dp[m] != m)
37         printf("No Solution");                // 无解
38     else{
39         // 记录最优路径
40         int k = n, num = 0, v = m;
41         while (k >= 0){
42             if (choice[k][v] == 1){
43                 flag[k] = true;
44                 v -= w[k];
45                 num++;
46             }
47             else{
48                 flag[k] = false;
49             }
50             k--;
51         }
52 
53         // 输出方案
54         for (int i = n; i >= 1; i--){
55             if (flag[i] == true){
56                 printf("%d", w[i]);
57                 num--;
58                 if (num > 0)
59                     printf(" ");
60             }
61         }
62     }
63 
64     // fclose(stdin);
65     return 0;
66 }

 

以上是关于动态规划 -- 01背包问题和完全背包问题的主要内容,如果未能解决你的问题,请参考以下文章

动态规划第五篇:01背包问题和完全背包问题

动态规划第五篇:01背包问题和完全背包问题

动态规划——背包问题python实现(01背包完全背包多重背包)

动态规划背包问题 01背包 完全背包 多重背包

代码随想录 动态规划 || 完全背包基础 518 377

动态规划问题3--多重背包