動態規划 -- 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 104​​ 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 (104​​, the total number of coins) and M (102​​, 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 V1​​V2​​Vk​​ such that V1​​+V2​​++Vk​​=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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM