您的位置:首页 > Web前端

POJ 3260 The Fewest Coins (混合背包--多重背包+完全背包)

2013-04-10 21:01 561 查看
The Fewest Coins

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3846 Accepted: 1141
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 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 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 (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 V1, V2, ..., VN coins (V1, ...VN) Line 3: N space-separated integers, respectively C1, C2, ..., 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

思路:顾客的硬币有限,属于多重背包,店主有无穷多的硬币,属于完全背包。令dp[x]表示价值为x的时候,最少的硬币个数。





View Code

1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 #define INF 0xfffffff
6 #define MAX 25005
7 #define SIZE 125
8 using namespace std;
9
10 int dp[MAX],dp2[MAX];
11 int v[SIZE],c[SIZE];
12 int N,M;
13
14 void multiPack(int vv,int cc)
15 {
16     int k = 1;
17     int num = cc;
18     while(k <= num)
19     {
20         for(int i=M+10000; i>=k*vv; i--)
21             dp[i] = min(dp[i],dp[i-k*vv]+k);
22         num -= k;
23         k *= 2;
24     }
25     if(num)
26     {
27         for(int i=M+10000; i>=num*vv; i--)
28             dp[i] = min(dp[i],dp[i-num*vv]+num);
29     }
30 }
31
32 int main()
33 {
34     while(~scanf("%d %d",&N, &M))
35     {
36         for(int i=1; i<=N; i++)
37             scanf("%d",&v[i]);
38         for(int i=1; i<=N; i++)
39             scanf("%d",&c[i]);
40         for(int i=1; i<=M+10000; i++)
41             dp[i] = dp2[i] = INF;
42         dp[0] = dp2[0] = 0;
43         for(int i=1; i<=N; i++)
44         {
45             multiPack(v[i],c[i]);
46         }
47         for(int i=1; i<=N; i++)
48         {
49             for(int j=v[i]; j<=M+10000; j++)
50                 dp2[j] = min(dp2[j],dp2[j-v[i]]+1);
51         }
52         int ans = INF;
53         for(int i=M; i<=M+10000; i++)
54             ans = min(ans,dp[i]+dp2[i-M]);
55         i
9b38
f(ans == INF)
56             printf("-1\n");
57         else
58             printf("%d\n",ans);
59     }
60     return 0;
61 }


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: