Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11230 Accepted Submission(s): 5677
all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to
be paid.
But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility
is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank
that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!
are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are
exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams.
cannot be reached exactly, print a line "This is impossible.".
3 10 110 2 1 1 30 50 10 110 2 1 1 50 30 1 6 2 10 3 20 4
The minimum amount of money in the piggy-bank is 60. The minimum amount of money in the piggy-bank is 100. This is impossible.
题解
完全背包的转移方程还是很好理解的,就是去判断,拿不拿这个物品。当然实现的做法有很多,一般的二维数组,01滚动,就地滚动等等。这个题是非常简单的背包题,就不多提了。
代码示例
/****************************************************************************** * COPYRIGHT NOTICE * Copyright (c) 2014 All rights reserved * ----Stay Hungry Stay Foolish---- * * @author :Shen * @name :[NWPU][2014][TRN][7] A * @file :G:\My Source Code\【ACM】训练\[NWPU][2014][TRN][7][0717]背包问题\A.cpp * @date :2014/07/17 10:54 * @algorithm :Package ******************************************************************************/ //#define _CRT_SECURE_NO_WARNINGS //#pragma GCC optimize ("O2") //#pragma comment(linker, "/STACK:1024000000,1024000000") //#include <bits/stdc++.h> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; template<class T>inline bool updateMin(T& a, T b){ return a > b ? a = b, 1 : 0; } template<class T>inline bool updateMax(T& a, T b){ return a < b ? a = b, 1 : 0; } const int INF = 0x07ffffff; const int MaxW = 10005; const int MaxN = 505; int dp[MaxW], p[MaxN], w[MaxN]; int t, n, v, E, F; // 完全背包 // 转移方程: // dp[j] = min(dp[j], dp[j - w[i]] + p[i]) void solve() { scanf("%d%d%d", &E, &F, &n); v = F - E; fill(dp + 1, dp + v + 1, INF); dp[0] = 0; // for (int i = 1; i <= v; i++)dp[i] = INF; dp[0] = 0; for (int i = 0; i < n; i++) scanf("%d%d", &p[i], &w[i]); for (int i = 0; i < n; i++) for (int j = w[i]; j <= v; j++) updateMin(dp[j], dp[j - w[i]] + p[i]); if (dp[v] == INF) puts("This is impossible."); else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[v]); } int main() { scanf("%d", &t); while (t--) solve(); return 0; }