POJ 3260 The Fewest Coins

Time Limit: 2000MS
Memory Limit: 65536K

Total Submissions: 2677
Accepted: 796

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 carryingC1 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

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.

这道题是一个背包问题,完全背包+多重背包,我已开始就想到了这一点,但是写完之后却超时,稍作修改之后却WA,后来在网上看了大牛的代码重新找到一种更好的方法。

我的代码:

#include <iostream>
#include <cstring>
using namespace std;

int dp1[1200005],dp2[1200005];
int Money_Sum,Money_Max;
int Money[105],Money_Count[105];
int N,T;

void DP1(int Sum,int temp)
{
    int j;
    for(j=Money_Sum;j>=Sum;j--)
    {
        dp1[j]=min(dp1[j],dp1[j-Sum]+temp);
    }
}

void DP2()
{
    int i,j;
    for(i=0;i<N;i++)
    {
        for(j=Money[i];j<=Money_Max*Money_Max;j++)
        {
            dp2[j]=min(dp2[j],dp2[j-Money[i]]+1);
        }
    }
}

void solve()
{
    int i,j,Min;
    for(i=0;i<=Money_Sum;i++)
    {
        dp1[i]=dp2[i]=100000000;
    }
    dp1[0]=dp2[0]=0;
    for(i=0;i<N;i++)
    {
        j=1;
        while(1)
        {
            if(Money_Count[i]-j<=0)
            {
                DP1(Money[i]*Money_Count[i],Money_Count[i]);
                break;
            }
            Money_Count[i]-=j;
            DP1(j*Money[i],j);
            j*=2;
        }
    }
    DP2();
    Min=100000000;
    for(i=T;i<=Money_Sum;i++)
    {
        Min=min(Min,dp1[i]+dp2[i-T]);
    }
    if(Min==100000000)
    {
        cout<<"-1"<<endl;
    }
    else
    {
        cout<<Min<<endl;
    }
}

int main()
{
    int i;
    while(cin>>N>>T)
    {
        Money_Max=0;
        for(i=0;i<N;i++)
        {
            cin>>Money[i];
            Money_Max=max(Money_Max,Money[i]);
        }
        for(i=0;i<N;i++)
        {
            cin>>Money_Count[i];
        }
        Money_Sum=T+Money_Max*Money_Max;
        solve();
    }
    return 0;
}

再把大牛的代码也贴上吧:

  1. //f1[i]表示支付i元所需的最小硬币数
  2. //f2[i]表示找i元所需的最小硬币数
  3. //买T元东西所需的最小硬币数为f1[T + i] - f2[i]
  4. //f1[i] = min(f1[i], f1[i - k] + 1)
  5. //f2[i] = min(f2[i], f2[i - k] + 1)
  6. //其中f1[i]多重背包,用二进制拆分求解
  7. //f2[i]为完全背包
  8. //其中:给钱上界为:T+maxValue^2,其中maxValue为最大硬币面值。
  9. //证明:反证法。假设存在一种支付方案,John给的钱超过T+maxValue^2,
  10. //则售货员找零超过maxValue^2,则找的硬币数目超过maxValue个,将其看作一数列,
  11. //求前n项和sum(n),根据鸽巢原理,至少有两 个对maxValue求模的值相等,
  12. //假设为sum(i)和sum(j),i<j,则i+1...j的硬币面值和为maxValue的倍数,
  13. //同理,John给的钱中也有 一定数量的硬币面值和为maxValue的倍数,
  14. //则这两堆硬币可用数量更少的maxValue面值硬币代替,产生更优方案。
  15. #include <iostream>
  16. #include <cmath>
  17. #include <cstring>
  18. using namespace std; 
  19. const int MAXN = 101; 
  20. const int MAXT = 10000 + 120 * 120 + 1; 
  21. const int INF = 200000000; 
  22. int f1[MAXT]; 
  23. int f2[MAXT]; 
  24. int v[MAXN]; 
  25. int c[MAXN]; 
  26. int main() { 
  27. //freopen("1.txt", "r", stdin);
  28. int n, T; 
  29. while(cin >> n >> T) { 
  30. int maxV = 0; 
  31. for(int i = 0; i < n; i++) { 
  32.             cin >> v[i]; 
  33.             maxV = max(maxV, v[i]); 
  34.         } 
  35.         maxV *= maxV; 
  36. int maxT = T + maxV; 
  37. for(int i = 0; i < n; i++) 
  38.             cin >> c[i]; 
  39. for(int i = 0; i <= maxT; i++) 
  40.             f1[i] = f2[i] = INF; 
  41.         f1[0] = f2[0] = 0; 
  42. //完全背包求解
  43. for(int i = 0; i < n; i++) { 
  44. for(int j = v[i]; j < maxV; j++) { 
  45.                 f2[j] = min(f2[j], f2[j - v[i]] + 1); 
  46.             } 
  47.         } 
  48. //多重背包求解
  49. for(int i = 0; i < n; i++) { 
  50. int k = 1; 
  51. int sum = 0; 
  52. while(sum < c[i]) { 
  53. for(int j = maxT; j >= v[i] * k; j--) { 
  54.                     f1[j] = min(f1[j], f1[j - v[i] * k] + k); 
  55.                 } 
  56.                 sum += k; 
  57. if(sum + k * 2 > c[i]) 
  58.                     k = c[i] - sum; 
  59. else
  60.                     k *= 2; 
  61.             } 
  62.         } 
  63. int _min = INF; 
  64. for(int i = T; i <= maxT; i++) { 
  65.             _min = min(_min, f1[i] + f2[i - T]); 
  66.         } 
  67. if(_min == INF) 
  68.             printf("-1/n"); 
  69. else
  70.             printf("%d/n", _min); 
  71.     } 
  72. return 0; 

posted on 2012-02-25 15:03  lzm风雨无阻  阅读(341)  评论(0编辑  收藏  举报

导航