poj 1276 Cash Machine (多重背包)

來源:互聯網
上載者:User

標籤:多重背包

連結:poj 1276

題意:已知金額cash,給定幾種不同面值的貨幣的數量及面值,求利用給定的貨幣可以湊成

小於等於cash的金額的最大值

分析:因為每種貨幣的面值及數量已知,可以將其轉化為多重背包,背包的容量即為cash,

每個物品的價值及費用都為每種貨幣的面值。

多重背包可以轉化為01背包,不過這樣會逾時,為了避免這樣,可以轉化為完全背包和二進位思想的01背包

#include<stdio.h>#include<string.h>int f[100010],v;int max(int a,int b){    return a>b?a:b;}void zeroone(int cost,int weight)  //01背包{    int i;    for(i=v;i>=cost;i--)        f[i]=max(f[i],f[i-cost]+weight);}void complete(int cost,int weight)       //完全背包{    int i;    for(i=cost;i<=v;i++)        f[i]=max(f[i],f[i-cost]+weight);}void multiple(int num,int cost,int weight)      //多重背包{    int k;    if(num*weight>=v){        complete(cost,weight);        return ;    }    for(k=1;k<num;k*=2){        zeroone(k*cost,k*weight);        num-=k;    }    zeroone(num*cost,num*weight);}int main(){    int i,n,num[1010],w[1010];    while(scanf("%d",&v)!=EOF){        scanf("%d",&n);        for(i=1;i<=n;i++)            scanf("%d%d",&num[i],&w[i]);        memset(f,0,sizeof(f));        for(i=1;i<=n;i++)            multiple(num[i],w[i],w[i]);          printf("%d\n",f[v]);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.