DP Full Backpack

Source: Internet
Author: User
Tags empty min
/*
Topic 1454:piggy-bank
Title Description:
Before ACM can do anything, a budget must is prepared and the necessary financial support obtained. The main income for this action comes from irreversibly Bound money (IBM). The idea behind are simple. Whenever some ACM member have any small money, he takes all the coins and throws them into a piggy-bank. You know it is irreversible and the coins cannot be removed without breaking the pig. After a sufficiently long time, there should is enough cash in the Piggy-bank to pay everything that needs to be paid.
But there was a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might the "the pig into Pieces" only "find out" that there are not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility was to weigh the piggy-bank, and try to guess what many coins is inside. Assume that we is able to determine the weight of the pig exactly and so we know the weights of all coins of a given CU Rrency. Then there are some minimum amount of money in the Piggy-bank so 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!
Input:
The input consists of T test cases. The number of them (T) is given in the first line of the input file. Each test case is begins with a line containing the integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights is given in grams. No Pig would weigh more than ten kg, that means 1 <= E <= F <= 10000. On the second line of all test case, there is a integer number N (1 <= n <=) that gives the number of various Coins used in the given currency. Following this is exactly N lines, each specifying one coin type. These lines contain, integers each, pand w (1 <= P <= 50000, 1 <= w <=10000). P is the value of the coin in monetary units, and W is it's weight in grams.
Output:
Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the Piggy-bank are X." where x is the minimum amount of Money that can is achieved using coins with the given total weight. If the weight cannot was reached exactly, print a line "This is impossible."
Sample input:
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
Sample output:
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.
*/
Full backpack: There is a backpack with a volume of V, while there are n items, each item has its own volume W and
Value V, the number of each item is unlimited, to use the backpack can be used up to the value of the sum of items
Full backpack, endless filling.
#include <stdio.h>
#define INF 0x7fffffff
int min (int a, int b) {return a < b? A:b;} Take the minimum value function
struct e{//represents a coin structure body
int w;//Weight
int v;//Value
}LIST[501];
int dp[10001];//State
int main () {
int T;
scanf ("%d", &t);//input test Data Group number
while (t--) {//t cycles, processing T-group data
int s,tmp,i,j;
scanf ("%d%d", &tmp,&s);//input empty storage tank weight and storage tank weight filled with coins
S-= tmp;//calculates the weight of the coin
int n;
scanf ("%d", &n);
for (i = 1;i<=n;i++) {
scanf ("%d%d", &AMP;LIST[I].V,&AMP;LIST[I].W);
}//input data
for (i = 0;i<=s;i++) {
Dp[i] = INF;
}
Dp[0] = 0;//Because all items are required to be filled exactly, so the original, except Dp[0], the rest of the dp[j] are infinite
for (i = 1;i<=n;i++) {//Traverse all items
for (j = list[i].w;j<=s;j++) {//full backpack, sequential traversal of all potentially transferred states
if (DP[J-LIST[I].W]! = INF)//If DP[J-LIST[I].W] is not infinite, it can be transferred from this state
Dp[j] = min (DP[J],DP[J-LIST[I].W] + list[i].v);//Take the transfer value and the lower value of the original value
}
}
if (dp[s]! = INF)//If there is a scheme for the backpack to fill up, output its minimum value
printf ("The minimum amount of money on the Piggy-bank is%d.\n", Dp[s]);
else//If no scenario exists
Puts ("This is impossible.");
}
return 0;
}
/*
0-1 backpacks, each item is available in 0 or 1, full backpack, each item can be selected for unlimited number of
, the two solutions are basically the same, and the time-space complexity is basically the same as the order of the state updating.
*/
/*
Multiple backpacks, in between, have a volume of V backpack, given some items, each item contains the volume of W,
Value V and Quantity K, the maximum amount of value that can be loaded with the backpack. The number of optional items is K
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.