当前位置: 代码迷 >> 综合 >> HDU 1114 Piggy-Bank
  详细解决方案

HDU 1114 Piggy-Bank

热度:71   发布时间:2023-11-06 18:08:21.0

题意:

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

#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.");elseprintf("The minimum amount of money in the piggy-bank is %d.\n", f[c]);}return 0; 
}


  相关解决方案