zoukankan      html  css  js  c++  java
  • POJ3260The Fewest Coins[背包]

    The Fewest Coins
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6299   Accepted: 1922

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


    题意:FJ每种硬币有限,售货员无限,最小化交易用的硬币数

    FJ多重背包,售货员完全背包
    min一下f[i+m]+d[i]
    NOTICE:体积选多大呢?
    有一个证明,如果John的付款数大于了maxv*maxv+m,即付硬币的数目大于了maxv,根据鸽笼原理,至少有两个的和对maxv取模的值相等,也就是说,这部分硬币能够用更少的maxv来代替。证毕。
    看不懂算了
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int N=105,M=120*120+1e4+5,INF=1e9;
    int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    int n,m,om,v[N],c[N],ans=INF,mxv=0;
    int f[M],d[M];
    inline void zp(int v,int w){
        for(int j=m;j>=v;j--) f[j]=min(f[j],f[j-v]+w);
    }
    inline void cp(int v){
        for(int j=v;j<=m;j++) f[j]=min(f[j],f[j-v]+1);
    }
    inline void mp(int v,int c){
        if(v*c>=m){cp(v);return;}
        int k=1;
        while(k<c){
            zp(k*v,k);
            c-=k;
            k*=2;
        }
        zp(c*v,c);
    }
    int main(){
        n=read();m=om=read();
        for(int i=1;i<=n;i++) v[i]=read(),mxv=max(mxv,v[i]);m+=mxv*mxv;
        for(int i=1;i<=n;i++) c[i]=read();
        
        for(int i=1;i<=m;i++) d[i]=INF;d[0]=0;
        for(int i=1;i<=n;i++) 
            for(int j=v[i];j<=m;j++) 
                d[j]=min(d[j],d[j-v[i]]+1);    
        for(int i=1;i<=m;i++) f[i]=INF;f[0]=0;
        for(int i=1;i<=n;i++) mp(v[i],c[i]);
        for(int i=0;i<=m-om;i++)
            if(f[i+om]+d[i]<ans) ans=f[i+om]+d[i];
        if(ans>=INF) printf("-1");
        else printf("%d",ans);
    }
     
  • 相关阅读:
    每日一博文
    用flash builder创建手机项目以及发布app需要注意的细节
    用xml配置加载cs中为ActionScript导出的类
    cs里面层,帧的处理方法
    一个项目要被自己加载的swf的项目调用方法
    App调用Android设备本地相机拍照并保存到本地相册
    让SWF文件从原始保存位置拿出来到任意位置都可以播放的设置
    记事
    一个主项目调用被加载的小游戏SWF项目的方法
    从一个App退出,关闭app
  • 原文地址:https://www.cnblogs.com/candy99/p/5901601.html
Copyright © 2011-2022 走看看