HDU-1114 Piggy-Bank 完全背包

简单完全背包。

代码如下:

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <cstring>

#include <iostream>

#include <algorithm>

#define MAXN 500

#define INF 0x3ffffff

using namespace std;



int E, F, N, W, dp[10005];



struct Node

{

    int p, w;    

}e[MAXN+5];



void c_bag(int x)

{

    for (int i = e[x].w; i <= W; ++i) {

        if (dp[i-e[x].w] != INF) {

            dp[i] = min(dp[i], dp[i-e[x].w]+e[x].p);

        //    printf("i=%d, dp=%d\n", i, dp[i]);

        }

    }

}



void DP()

{

    for (int i = 1; i <= N; ++i) {

        c_bag(i);

    }  

}



int main()

{

    int T;

    scanf("%d", &T);

    while (T--) {

        scanf("%d %d", &E, &F);

        W = F - E;

        dp[0] = 0;

        for (int i = 1; i <= W; ++i) {

            dp[i] = INF;

        }

        scanf("%d", &N);

        for (int i = 1; i <= N; ++i) {

            scanf("%d %d", &e[i].p, &e[i].w);

        }

        DP();

        if (dp[W] == INF) {

            puts("This is impossible.");

        }

        else {

            printf("The minimum amount of money in the piggy-bank is %d.\n", dp[W]);

        }

    }

    return 0;

}

 

你可能感兴趣的:(pig)