题目: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]); } }
时间: 2024-09-29 16:16:15