POJ 1384 Piggy-Bank DP

一个完全背包 很裸,对于我这种DP渣渣都能1A。。

// by SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int cases,n,E,F,f[20050],w[505],v[505];
int main(){
    scanf("%d",&cases);
    while(cases--){
        memset(f,0x3f,sizeof(f)),f[0]=0;
        scanf("%d%d",&E,&F),E=F-E;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d%d",&w[i],&v[i]);
        if(E<0){puts("This is impossible.");continue;}
        for(int i=0;i<=E;i++)
            for(int j=1;j<=n;j++)
                f[i+v[j]]=min(f[i+v[j]],f[i]+w[j]);
        if(f[E]!=0x3f3f3f3f)printf("The minimum amount of money in the piggy-bank is %d.\n",f[E]);
        else puts("This is impossible.");
    }
}

这里写图片描述

posted @ 2016-07-29 23:23  SiriusRen  阅读(154)  评论(0编辑  收藏  举报