zoukankan      html  css  js  c++  java
  • POJ 3260 The Fewest Coins


    The Fewest Coins
    Time Limit: 2000MSMemory Limit: 65536K
    Total Submissions: 3959Accepted: 1174

    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 V1V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1C2coins 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 V1V2, ..., VN coins (V1, ...VN
    Line 3: N space-separated integers, respectively C1C2, ..., 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.

    Source


    顾客凑齐i需要的最少硬币个数 (多重背包)
    店主凑齐i需要的最少硬币个数 (完全背包)
    最后统计最少需要多少个硬币。。。。。判断不好上界所以数组开大点 


    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    const int INF=0x3f3f3f3f;
    int V;

    int a[111],c[111];

    int dps[20010];
    int dpf[20010];

    void zpack(int* a,int c,int w)
    {
        for(int i=V;i>=c;i--)
        {
            a=min(a,a[i-c]+w);
        }
    }

    void cpack(int* a,int c,int w)
    {
        for(int i=c;i<=V;i++)
        {
            a=min(a,a[i-c]+w);
        }
    }

    void multipack(int* a,int c,int w,int m)
    {
        if(c*m>=V)
        {
            cpack(a,c,1);
            return ;
        }
        int k=1;
        while(k<m)
        {
            zpack(a,c*k,k*w);
            m-=k;
            k*=2;
        }

        zpack(a,c*m,m*w);
    }

    int main()
    {
        int N,T;
        cin>>N>>T;
        for(int i=0;i<N;i++)
            cin>>a;
        for(int i=0;i<N;i++)
            cin>>c;

        for(int i=0;i<T+10000;i++)
            dps=dpf=INF;
        dps[0]=dpf[0]=0;

        V=T+10000;

        for(int i=0;i<N;i++)
            cpack(dps,a,1);

        for(int i=0;i<N;i++)
            multipack(dpf,a,1,c);

        int ans=INF;
        for(int i=T;i<=V;i++)
            ans=min(ans,dpf+dps[i-T]);

        if(ans>=INF) cout<<-1<<endl;
        else
        cout<<ans<<endl;

        return 0;
    }

  • 相关阅读:
    Windows Phone开发31日谈
    Log4Net(二)
    依赖注入容器Autofac的详解
    Windows Phone 学习教程(一)
    Fiddler教程
    MongoDb笔记(一)
    poj 1144 Network
    poj 3185 The Water Bowls
    poj 1753 Flip Game
    poj 2065 SETI
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351026.html
Copyright © 2011-2022 走看看