The fewest coins
| Time limit:2000 ms |
|
Memory limit:65536 K |
| Total submissions:2517 |
|
Accepted:738 |
Description Farmer John has gone to town to buy some farm supplies. being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, I. E ., the number of coins he uses to pay plus the number of coins he has es In change is minimized. Help him to determine what this minimum number is. FJ wants to buyT(1 ≤T≤ 10,000) cents of supplies. The currency system hasN(1 ≤N≤ 100) different coins, with valuesV1,V2 ,...,Vn(1 ≤VI≤ 120). Farmer John is carryingC1 coins of ValueV1,C2 Coins of ValueV2,..., andCNCoins of ValueVn(0 ≤Ci≤ 10,000 ). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner (although Farmer John must be sure to pay in a way that makes it possible to make the correct change ). Input Line 1: two space-separated integers: N And T . Line 2: n space-separated integers, respectively V 1, V 2 ,..., Vn Coins ( V 1 ,... Vn ) Line 3: n space-separated integers, respectively C 1, C 2 ,..., 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 exact change, output-1.Sample Input 3 705 25 505 2 1 Sample output 3 Hint Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and distinct es a 5 cents coin in change, for a total of 3 Coins used in the transaction. Source Usaco 2006 December gold |
Question: http://poj.org/problem? Id = 3260
Analysis: This question is like a previous question, but it turns into a full backpack. The key to this question is the estimation of the scope of payment. Some people on the Internet say it is maxv * maxv + T, but I won't estimate =. It's like this... However, we still want to pass the data up to around t...
Code:
# Include <cstdio> # include <iostream> using namespace STD; const int Mm = 24222; const int Mn = 111; int V [Mn], C [Mn]; int f [mm], G [mm]; int n, m, MV, MX; void completepack (INT v) {for (INT I = V; I <= mV; ++ I) f [I] = min (F [I], F [I-v] + 1);} void zeroonepack (INT V, int D) {for (INT I = mV; I> = V; -- I) f [I] = min (F [I], F [I-v] + d );} int main () {int I, j; while (scanf ("% d", & N, & M )! =-1) {for (MX = I = 0; I <n; ++ I) scanf ("% d", & V [I]), MX = max (MX, V [I]); for (I = 0; I <n; ++ I) scanf ("% d", & C [I]); MX = Mx * MX; MV = m + mx; for (I = 0; I <= mV; ++ I) f [I] = G [I] = mm; f [0] = G [0] = 0; for (I = 0; I <n; ++ I) if (C [I]) {If (C [I] * V [I]> = mV) completepack (V [I]); else {j = 1; while (j <C [I]) {zeroonepack (V [I] * j, J); C [I]-= J; j <= 1;} zeroonepack (V [I] * C [I], c [I]) ;}}for (I = 0; I <n; ++ I) for (j = V [I]; j <= Mx; ++ J) G [J] = min (G [J], G [J-V [I] + 1); for (I = m; I <= mV; ++ I) f [m] = min (F [m], F [I] + G [I-m]); printf ("% d \ n", F [m] <mm? F [m]:-1);} return 0 ;}