POJ 1384

求猜存钱罐中至少有多少钱。容易知道金币总的重量,接着背包。

#include<cstdio>

#include<iostream>

using namespace std;

#define N 10010

#define M 100000000





int dp[N];

int val[501],we[501];



int main()

{

    int t;

    scanf("%d",&t);

    while(t--)

    {

        int em,fu;

        scanf("%d%d",&em,&fu);

        int n;

        scanf("%d",&n);

        for(int i=0; i<n; i++)

            scanf("%d%d",&val[i],&we[i]);

        int ff=fu-em;

        for(int i=0; i<=ff; i++)

            dp[i]=M;

        dp[0]=0;



        for(int i=0; i<n; i++)

        {

            for(int j=we[i]; j<=ff; j++)

                dp[j]=min(dp[j],dp[j-we[i]]+val[i]);

        }

        if(dp[ff] ==M)printf("This is impossible.\n");

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



    }

    return 0;

}

 

你可能感兴趣的:(poj)