poj 3260 The Fewest Coins

poj 3260 The Fewest Coins,第1张

poj 3260 The Fewest Coins
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int v[105],c[105],MAX,n,sum;int dp[33333],inf = 100000000;void ZeroonePack(int cost,int cnt){    int i;    for(i = sum+MAX; i>=cost; i--)        dp[i] = min(dp[i],dp[i-cost]+cnt);}void CompletePack(int cost,int cnt){    int i;    for(i = sum+MAX+cost; i>=0; i--)        dp[i] = min(dp[i],dp[i-cost]+cnt);}int MultiplePack(){    int i,j,k;    for(i = 1; i<=sum+MAX; i++)        dp[i] = inf;    dp[0] = 0;    for(i = 1; i<=2*n; i++)    {        if(i<=n)        { k = 1; while(k<c[i]) {     ZeroonePack(k*v[i],k);     c[i]-=k;     k*=2; } ZeroonePack(c[i]*v[i],c[i]);        }        else CompletePack(-v[i-n],1);    }    if(dp[sum]==inf)        return -1;    else        return dp[sum];}int main(){    int i;    while(~scanf("%d%d",&n,∑))    {        MAX = 0;        for(i=1; i<=n; i++)        { scanf("%d",&v[i]); MAX = max(MAX,v[i]);        }        MAX*=MAX;        for(i=1; i<=n; i++) scanf("%d",&c[i]);        printf("%dn",MultiplePack());    }    return 0;}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4895923.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存