标签:return nsa 多重背包 soft stream ems rect 没有 algorithm
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 receives in 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 C1coins 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 (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).
Input
Output
Sample Input
3 70
5 25 50
5 2 1
Sample Output
3
Hint
1 #include <iostream>
2 #include <stdio.h>
3 #include <string.h>
4 #include<math.h>
5 #include <algorithm>
6 using namespace std;
7
8 const int MAX = 40000;
9 int N,M;
10 int a[MAX],num[MAX],dp1[MAX],dp2[MAX],dp3[MAX];
11 int main()
12 {
13 while(~scanf("%d %d",&N,&M))
14 {
15 for(int i=1;i<=N;i++)
16 scanf("%d",&a[i]);
17 for(int i =1;i<=N;i++)
18 scanf("%d",&num[i]);
19
20 for(int i=1;i<=MAX;i++)
21 dp1[i]=dp2[i]=MAX*1000;
22 dp1[0]=dp2[0]=0;
23 for(int i=1;i<=N;i++)
24 {
25 memset(dp3,0,sizeof(dp3));
26 for(int j=a[i];j<=MAX;j++)
27 {
28 if(dp1[j]>dp1[j-a[i]]+1&&dp3[j-a[i]]<num[i])
29 {
30 dp1[j] = dp1[j-a[i]]+1;
31 dp3[j] =dp3[j-a[i]] +1;
32 }
33 dp2[j] = min(dp2[j],dp2[j-a[i]]+1);
34 }
35 }
36
37 int minx=MAX*1000;
38 for(int i = M;i<=MAX;i++)
39 {
40 minx = min(minx,dp1[i]+dp2[i-M]);
41 }
42 if(minx>=MAX*1000)
43 printf("-1\n");
44 else
45 printf("%d\n",minx);
46
47
48 }
49
50 return 0;
51 }
标签:return nsa 多重背包 soft stream ems rect 没有 algorithm
原文地址:http://www.cnblogs.com/a2985812043/p/7375671.html