poj1384

//
//  main.cpp
//  poj1384
//
//  Created by 韩雪滢 on 12/13/16.
//  Copyright © 2016 韩雪滢. All rights reserved.
//

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int T;
    cin >> T;
    for(int i=0;i<T;i++){
        int E,F,N;
        cin >> E >> F;
        
        int *allweight = new int[F-E+1];
        allweight[0] = 0;
        for(int k=1; k<F-E+1;k++)
            allweight[k] = 10000000;
        
        cin >> N;
        for(int j=0;j<N;j++){
            int P,W;
            cin >> P >> W;
            for(int n=W;n<=F-E;n++)
                allweight[n] = min(allweight[n],allweight[n-W]+P);

            
        }
        if(allweight[F-E] != 10000000){
        cout <<"The minimum amount of money in the piggy-bank is " <<allweight[F-E] << "."<< endl;
        }else{
            cout << "This is impossible." << endl;
        }
    }
    return 0;
}

 

posted @ 2016-12-13 21:02  ShellHan  阅读(264)  评论(0编辑  收藏  举报