The trouble of Xiaoqian
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1023 accepted submission (s): 335
Problem descriptionin the country of alpc, Xiaoqian is a very famous mathematician. she is immersed in calculate, And she want to use the minimum number of coins in every shopping. (the numbers of the shopping include the coins she gave the store and
The store backed to her .)
And now, Xiaoqian 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 ). xiaoqian is carrying C1 coins of value V1, C2 coins of value V2 ,...., and CN coins
Value VN (0 ≤ci ≤10,000 ). the shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner. but Xiaoqian is a low-pitched girl, she wouldn't like giving out more than 20000 once.
Inputthere are several test cases in the input.
Line 1: two space-separated integers: N and T.
Line 2: n space-separated integers, respectively V1, V2,..., vn coins (V1,... VN)
Line 3: n space-separated integers, respectively C1, C2,..., CN
The end of the input is a double 0.
Outputoutput one line for each test case like this "case X: Y": X presents the Xth test case and Y presents the minimum number of coins. if it is impossible to pay and receive exact change, output-1.
Sample Input
3 705 25 505 2 10 0
Sample output
Case 1: 3
The currency system has n different denominations of money. The values of each currency are V1, V2 ,..., vn: a person wants to buy value and t goods, and each of them carries C1, C2 ,..., CN, then ask you the minimum number of coins you need to handle after the transaction is completed
Train of Thought: carry out multiple Backpacks for people first, and then carry out a full backpack for Salespersons
The money that the person brought is just enough to make up t, so there is no need to find the money, otherwise it will need to find the money, which is equivalent to the man who paid the money that he could pay more than T m, then the salesman looked for the money M-T and added up the amount of money for both.
In addition to DP [0] = 0 during initialization, all others indicate that the I status can be combined by products.
Otherwise, if all initialization is 0, I can only be an upper bound.
Import Java. io. *; import Java. util. *; public class main {int max = 0 xfffffff, size = 20000, M = 101; int N, T; int [] dp1 = new int [size + 1]; int [] dp2 = new int [size + 1]; int [] val = new int [m]; int [] con = new int [m]; public static void main (string [] ARGs) {New Main (). work ();} void work () {vertex SC = new vertex (New bufferedinputstream (system. in); int case = 1; while (SC. hasnext () {n = SC. nextint (); t = SC. nextint (); If (n = 0 & t = 0) break; arrays. fill (Val, 0); arrays. fill (con, 0); For (INT I = 0; I <n; I ++) Val [I] = SC. nextint (); For (INT I = 0; I <n; I ++) con [I] = SC. nextint (); // multiple backpacks arrays. fill (dp1, max); dp1 [0] = 0; For (INT I = 0; I <n; I ++) multiplepack (Val [I], CON [I]); // complete backpack arrays. fill (dp2, max); dp2 [0] = 0; For (INT I = 0; I <n; I ++) for (Int J = Val [I]; j <= size; j ++) dp2 [J] = math. min (dp2 [J], dp2 [J-Val [I] + 1); int min = max; For (INT I = 0; I <n; I ++) {for (Int J = T; j <= size; j ++) min = math. min (Min, dp1 [J] + dp2 [J-T]);} If (min = max) min =-1; system. out. println ("case" + case ++ ":" + min) ;}} void multiplepack (INT Val, int num) {int K = 1; while (k <num) {zeroonepack (K, Val); num-= K; K * = 2;} zeroonepack (Num, Val);} void zeroonepack (int K, int Val) {val = K * val; For (INT I = size; I> = val; I --) {dp1 [I] = math. min (dp1 [I], dp1 [I-Val] + k );}}}