zoukankan      html  css  js  c++  java
  • poj 3260 最少硬币(01+多重+完全背包)

    http://www.cnblogs.com/ACMan/archive/2012/08/14/2637437.html

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <cctype>
    #include <vector>
    #include <iterator>
    #include <set>
    #include <map>
    #include <sstream>
    using namespace std;
    
    #define mem(a,b) memset(a,b,sizeof(a))
    #define pf printf
    #define sf scanf
    #define spf sprintf
    #define pb push_back
    #define debug printf("!
    ")
    #define MAXN 1010
    #define MAX(a,b) a>b?a:b
    #define blank pf("
    ")
    #define LL long long
    #define ALL(x) x.begin(),x.end()
    #define INS(x) inserter(x,x.begin())
    #define pqueue priority_queue
    #define INF 0x3f3f3f3f
    
    int n,T;
    
    const int mx = 30000;
    
    int dp[30000],f[30000],w[30000],c[30000];
    
    
    void zobag(int value,int weight)
    {
        for(int v = mx;v>=weight;v--)
        {
            dp[v] = min(dp[v],dp[v-weight]+value);
        }
    }
    
    void cmbag(int value,int weight)
    {
        for(int v = weight;v<=mx;v++)
        {
            dp[v] = min(dp[v],dp[v-weight]+value);
        }
    }
    
    void fcmbag(int value,int weight)
    {
        for(int v = weight;v<=mx;v++)
        {
            f[v] = min(f[v],f[v-weight]+value);
            //pf("v%d f%d
    ",v,f[v]);
        }
    }
    
    void mubag(int weight,int amount)
    {
        if(weight*amount >= mx)
        {
            cmbag(1,weight);
            return;
        }
        int k = 1;
        while(k < amount)
        {
            //pf("amt%d
    ",amount);
            //pf("k%d
    ",k);
            zobag(k,k*weight);
            amount-=k;
            k*=2;
        }
        zobag(amount,amount*weight);
    }
    
    int main()
    {
        int i,j;
        while(~sf("%d%d",&n,&T))
        {
            mem(dp,INF);
            mem(f,INF);
            dp[0]=0;
            for(i=0;i<n;i++)
            {
                sf("%d",&w[i]);
            }
            for(i=0;i<n;i++)
            {
                sf("%d",&c[i]);
            }
            for(i=0;i<n;i++)
            {
                mubag(w[i],c[i]);
            }
            f[0]=0;
            for(i=0;i<n;i++)
            {
                fcmbag(1,w[i]);
            }
            int ans = INF;
            for(i=0;i<mx-T;i++)
            {
                ans = min(ans,dp[i+T]+f[i]);
            }
            if(ans == INF) ans = -1;
            pf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Hadoop2.5.2 安装部署
    Hadoop1.0.3安装部署
    水滴石穿
    使用tfrecord建立自己的数据集
    tesonflow实现word2Vec
    python+opencv 图像预处理
    ubuntu14.0 更改默认python为3.5 并安装tensorflow(cpu)
    python3.5+win7 安装 numpy 和scipy的总结
    关于matlab GUI 的一些总结
    23333 又是一篇水文章(以下是各种复制来的关于maven转成eclipse项目)
  • 原文地址:https://www.cnblogs.com/qlky/p/5635001.html
Copyright © 2011-2022 走看看