HRBUST 1993【Complete Backpack Naked Title】

Question: Lili has many coins of different denominations, and the weights of coins of different denominations are also different. Now that you know the types and denominations of coins contained in this pile of coins, as well as the total weight of this pile of coins, you can calculate how much Lili is at least there. money. .

Just change max to min. .
In fact, I don't have a deep understanding of the complete backpack. T_T, I typed the code based on my impression.

#include<stdio.h>
#include<string.h>
#include<iostream>
#define MAX 1000000
#define INF 0x1f1f1f1f
using namespace std;

struct c {
    int w;
    int val;

}coin[100000];
int dp[MAX];
int min(int a, int b) {
    return (a > b) ? b : a;

}


int main(void) {
    int W, n;
    while (~scanf("%d%d", &W, &n)) {
        for (int i = 1; i <= n; i++)
            scanf("%d%d", &coin[i].val, &coin[i].w);

        memset(dp, INF, sizeof(dp));
        dp[0] = 0;

        for (int i = 1; i <= n; i++) {
            for (int v = coin[i].w; v <= W; v++) {
                dp[v] = min(dp[v], dp[v - coin[i].w] + coin[i].val);

            }

        }
        if (dp[W] == INF)
            printf("-1\n");
        else
            printf("%d\n", dp[W]);


    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325079011&siteId=291194637