poj3260--Backpack DP (multiple)--the fewest Coins

Source: Internet
Author: User

Description

Farmer John had gone to town to buy some farm supplies. Being a very efficient man, he is pays for his goods in such a, the the smallest number of coins changes hands, I. E., the number of coins he uses to pay plus the number of coins he receives on change is minimized. Help him to determine what this minimum number is.

FJ wants to buy t (1≤ T ≤10,000) cents of supplies. The currency system has n (1≤ n ≤100) different coins, with values V1, v2, ..., VN (1≤ Vi≤120). Farmer John is carrying C1 Coins of value v1, C2 coins of value v2, ...., and CN Coins of value VN (0≤ Ci ≤10,000). The shopkeeper has a unlimited supply of all the coins, and always makes change in the most efficient manner (although Fa Rmer John must is sure to pay in a-to-makes it possible to make the correct change).

Input

Line 1:two space-separated integers: Nand T.
Line 2:n space-separated integers, respectively V1, V2,..., VNCoins ( V1, ... VN)
Line 3:n space-separated integers, respectively C1, C2,..., CN

Output

Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If It is impossible for Farmer John to pay and receive the exact change, output-1.

Sample Input

3 705 25 505 2 1

Sample Output

3

Hint

Farmer John pays cents using a cents and a cents coin, and receives a 5 cents coin in change, for a total of 3 co Ins used in the transaction. The main idea: let you change, the least circulating currency, buy things when the currency is limited, change money when the currency is not limited, multiple knapsack problem with the binary 01 backpack easy to solve, with functions to write clearly more ~
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAX =50010;Const intINF =0x3f3f3f3f;intdp[50010];intw[ the],num[ the];intn,m;voidOneback (intWintv) {     for(inti = MAX; I >= W; i--) Dp[i]= Min (dp[i],dp[i-w]+v);}voidComback (intWintv) {     for(inti = max+w; i>=0; i--) Dp[i]= Min (dp[i],dp[i-w]+v);}intMultiplepack () {intK;  for(inti =1; I <= MAX; i++) Dp[i]=inf; dp[0] =0;  for(inti =1; I <=2*n;i + +){        if(I <=N) {k=1;  while(K <Num[i]) {Oneback (k*w[i],k); Num[i]-=K; K*=2; } oneback (Num[i]*W[i],num[i]); }        ElseComback (-w[i-n],1); }    if(Dp[m] = =inf)return-1; Else returndp[m];}intMain () { while(~SCANF ("%d%d",&n,&m)) {memset (DP,0,sizeof(DP));  for(inti =1; I <= N; i++) scanf ("%d",&W[i]);  for(inti =1; I <= N; i++) scanf ("%d",&Num[i]); printf ("%d\n", Multiplepack ()); }    return 0;}
View Code

poj3260--Backpack DP (multiple)--the fewest Coins

Related Article

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.