http://acm.hdu.edu.cn/showproblem.php?pid=1114

完全背包,求最小值

View Code
#include <iostream>
using namespace std ;
int V ;
int dp[1000001] ;
const int INF=0xfffffff  ;
void CompletePack(int c,int w)
{
    for(int i=c;i<=V;i++)
        dp[i]=min(dp[i],dp[i-c]+w) ;
    return ;
}
int c[50001],w[50001] ;
int main()
{
    int t ;
    scanf("%d",&t) ;
    while(t--)
    {
        int e,f ;
        scanf("%d%d",&e,&f) ;
        int n ;
        scanf("%d",&n) ;
        for(int i=0;i<n;i++)
            scanf("%d%d",&w[i],&c[i]) ;
        V=f-e ;
        if(!V)
        {
            printf("The minimum amount of money in the piggy-bank is 0.\n") ;
            continue ;
        }
        for(int i=1;i<=V;i++)
            dp[i]=INF ;
        dp[0]=0 ;
        for(int i=0;i<n;i++)
            CompletePack(c[i],w[i]) ;
        if(dp[V]==INF)
            puts("This is impossible.") ;
        else
            printf("The minimum amount of money in the piggy-bank is %d.\n",dp[V]) ;
    }
    return 0 ;
}