码迷,mamicode.com
首页 > 其他好文 > 详细

SPOJ Problem 77:Piggy-Bank

时间:2015-03-15 16:47:39      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

题目:http://www.spoj.com/problems/PIGBANK/

简单的背包问题。。

#include<cstdio>
#include<cstring>
#define INF 1000000005
int ew,fw,n,i,j,t,m;
int v[505],w[505],f[10005];
int min(int a,int b){return a<b?a:b;}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&ew,&fw);
        scanf("%d",&n);
        for (i=1;i<=n;i++)
            scanf("%d%d",&v[i],&w[i]);
        for (i=1;i<=fw-ew;i++)
            f[i]=INF;
        f[0]=0;
        for (i=1;i<=n;i++)
            for (j=w[i];j<=fw-ew;j++)
                f[j]=min(f[j],f[j-w[i]]+v[i]);
        if (f[fw-ew]==INF)printf("This is impossible.\n");
        else printf("The minimum amount of money in the piggy-bank is %d.\n",f[fw-ew]);
    }
}

 

SPOJ Problem 77:Piggy-Bank

标签:

原文地址:http://www.cnblogs.com/moris/p/4339908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!