HDU 1114

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

#include <bits/stdc++.h>
#define maxs 202020
#define mme(i,j) memset(i,j,sizeof(i))
using namespace std;
int w[maxs],h[maxs];
int dp[maxs];

int main(){
    int t,e,f;
    scanf("%d",&t);
    while(t--){

        scanf("%d%d",&e,&f);
        f= f-e;
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d%d",&w[i],&h[i]);
        for(int i=0 ;i<=f ;i++) dp[i] = maxs;
        dp[0]=0;
        for(int i=0;i<n;i++){
            for(int j=h[i];j<=f;j++){
                dp[j] = min(dp[j],dp[j-h[i]]+w[i]);
            }
        }

        if(dp[f]==maxs)
            puts("This is impossible");
        else
            printf("The minimum amount of money in the piggy-bank is %d.\n",dp[f]);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章