The trouble of Xiaoqian
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1977 Accepted Submission(s): 700
Problem Description
In 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 of 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.
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 of 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.
Input
There 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.
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.
Output
Output 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 70 5 25 50 5 2 1 0 0
Sample Output
Case 1: 3
Author
alpc97
Source
给钱用一次多重背包,(用最小硬币装满,可假设单个硬币价值为1,代价为它的面值),找钱用一次完全背包,遍历一遍找最小值即可。
# include <stdio.h> # include <string.h> # define INF 0x3f3f3f3f int min(int a, int b){return a<b?a:b;} int main() { int N, t, w=1, i, j, k, imin, flag, icount, v[101], n[101], value[15000], cost[15000], dp1[20001], dp2[20001]; while(1) { scanf("%d%d",&N, &t); if(N+t==0) break; icount = 1; imin = INF; for(i=1; i<=20000; ++i) dp1[i] = dp2[i] = INF; dp2[0] = dp1[0] = 0; for(i=1; i<=N; ++i) scanf("%d",&v[i]); for(i=1; i<=N; ++i) scanf("%d",&n[i]); for(i=1; i<=N; ++i) { for(k=1; k<=n[i]; k<<=1)//二进制优化 { value[icount] = k; cost[icount++] = k*v[i]; n[i] -= k; } if(n[i] > 0) { value[icount] = n[i]; cost[icount++] = n[i]*v[i]; } } for(i=icount-1; i>=1; --i)//给钱 for(j=20000; j>=cost[i]; --j) if(dp1[j-cost[i]]+value[i] < dp1[j]) dp1[j] = dp1[j-cost[i]]+value[i]; for(i=1; i<=N; ++i)//找钱 for(j=v[i]; j<=20000; ++j) if(dp2[j-v[i]]+1 < dp2[j]) dp2[j] = dp2[j-v[i]]+1; for(i=t; i<=20000; ++i) if(dp1[i] + dp2[i-t] < imin) imin = dp1[i] + dp2[i-t]; if(imin < INF) printf("Case %d: %d\n",w, imin); else printf("Case %d: -1\n",w); ++w; } return 0; }