HDU 1114 Piggy-Bank

71 阅读1分钟

题意:

  告诉你存钱罐中的钱的重量。 求钱的价值最少有多少。

#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; 
}


\

本文已参与「新人创作礼」活动,一起开启掘金创作之路