码迷,mamicode.com
首页 > 其他好文 > 详细

The Fewest Coins

时间:2017-08-16 21:39:45      阅读:234      评论:0      收藏:0      [点我收藏+]

标签: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 V1V2, ..., VN(1 ≤ Vi ≤ 120). Farmer John is carrying C1coins of value V1C2 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

Line 1: Two space-separated integers: N and T
Line 2: N space-separated integers, respectivelyV 1V 2, ..., VN coins ( V 1, ... VN
Line 3: N space-separated integers, respectivelyC 1C 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 70
5 25 50
5 2 1

Sample Output

3

Hint

Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.
 
题目意思:给出钱币的方案数和总价值,然后给出每种钱币的价值与数量,而老板也是每种钱币都拥有,但是没有数量限制,购买东西的时候,价值超过给定价值的话,老板会找钱,要求最小的交流钱币的数量
解题思路:多重背包和完全背包混合使用
 
 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 }



The Fewest Coins

标签:return   nsa   多重背包   soft   stream   ems   rect   没有   algorithm   

原文地址:http://www.cnblogs.com/a2985812043/p/7375671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!