POJ-1384(完全背包)(Piggy-Bank)

【题目描述】

已知储蓄罐满时的质量f以及空时质量e,有n种硬币,每种硬币的价值为p,质量为w,求该储蓄罐中的最少有多少钱?

int p[501], w[501];
int dp[50005];
int main()
{
	int cases;
	cin>>cases;
	while (cases--) {
		int e, f;
		cin>>e>>f;
		int W = f - e;
		int n, i, j;
		cin>>n;
		for (i = 0; i < n; ++i)
			cin>>p[i]>>w[i];
		for (i = 0; i <= W; ++i)
			dp[i] = 1000000;
		dp[0] = 0;
		for (i = 0; i < n; ++i) {
			for (j = w[i]; j <= W; ++j) {
				dp[j] = MY_MIN(dp[j - w[i]] + p[i], dp[j]);
			}
		}
		if (dp[W] == 1000000) printf("This is impossible.\n");
		else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[W]);
	}
    return 0;
}


你可能感兴趣的:(POJ-1384(完全背包)(Piggy-Bank))