HDU 1114 Piggy-Bank
原创
©著作权归作者所有:来自51CTO博客作者wx634fafb167087的原创作品,请联系作者获取转载授权,否则将追究法律责任
题意:
告诉你存钱罐中的钱的重量。 求钱的价值最少有多少。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int mx = 502;
const int inf = 5e8 + 10; //刚开始inf写小了
int f[11000], w[mx], v[mx]; // f也开小了
int main(){
int t, n, a, b, c;
scanf("%d", &t);
while(t--){
memset(f, 63, sizeof(f));
scanf("%d %d", &a, &b);
c = b - a;
scanf("%d", &n);
f[0] = 0;
for(int i = 1; i <=n; i++)
scanf("%d %d",&v[i], &w[i]);
for(int i = 1; i <= n; i++)
for(int m = w[i]; m <= c; m++){
f[m] = min( f[m - w[i]] + v[i], f[m]);
}
if(f[c] > inf)
puts("This is impossible.");
else
printf("The minimum amount of money in the piggy-bank is %d.\n", f[c]);
}
return 0;
}