POJ 1276 多重背包+多重背包可行化问题+单调队列优化

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example, 

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10 

means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each. 

Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine. 

Notes: 
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc. 

Input

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: 

cash N n1 D1 n2 D2 ... nN DN 

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct. 

Output

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 

Sample Input

735 3  4 125  6 5  3 350
633 4  500 30  6 100  1 5  0 1
735 0
0 3  10 100  10 50  10 10

Sample Output

735
630
0
0

Hint

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash. 

In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash. 

In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.

题意:开始输入一个M,N,M代表所需要的金额,N代表有几种钱,然后之后就是分别输入有numi张面值为ai的钱,最后让你求出用这些面值的钱最后可以组成最大不超过M的金额是多少;

明显的多重背包的板子:

下面使用int类型与bool类型(当数据大一点的话速度比int类型快一点)的AC

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const int MOD=1e6;
const int INF= 0x3f3f3f3f;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define init(a,b) memset(a,b,sizeof a)
int dp[N];
int a[200];
int num[200];
int w[200];
   int n,m;
   int ans;
void ZeroOne(int v,int weight){
    for(int i=m;i>=v;i--)
        dp[i]=max(dp[i],dp[i-v]+weight);
}
void Compelet(int v,int weight){
    for(int i=v;i<=m;i++)
        dp[i]=max(dp[i],dp[i-v]+weight);
}
void Multiply(int count,int val,int weigth){
    if(count*val>=m) {
            Compelet(val,weigth);return ;}
    for(int i=1;i<=count;){
        ZeroOne(val*i,weigth*i);
        count-=i;
        i<<=1;
    }
    if(count>0)
    ZeroOne(val*count,weigth*count);
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while(scanf("%d%d",&m,&n)!=EOF){
        rep(i,1,n)
        scanf("%d%d",&num[i],&a[i]),w[i]=a[i];
        init(dp,0);
        ans=0;
//        dp[0]=1;
        rep(i,1,n) Multiply(num[i],a[i],w[i]);//多重背包的的操作,一种变量赋予两种含义
        printf("%d\n",dp[m]);
    }
    return 0;
}
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const int MOD=1e6;
const int INF= 0x3f3f3f3f;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define init(a,b) memset(a,b,sizeof a)
bool dp[N];
int a[200];
int num[200];
   int n,m;
   int ans;
void ZeroOne(int v){
    for(int i=m;i>=v;i--)
        if(!dp[i]&&dp[i-v])
        dp[i]=1,ans=max(ans,i);
}
void Compelet(int v){
    for(int i=v;i<=m;i++)
        if(!dp[i]&&dp[i-v])
        dp[i]=1,ans=max(ans,i);
}
void Multiply(int count,int val){
    if(count*val>=m) {
            Compelet(val);return ;}
    for(int i=1;i<=count;){
        ZeroOne(val*i);
        count-=i;
        i<<=1;
    }
//    if(count>0)
    ZeroOne(val*count);
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while(scanf("%d%d",&m,&n)!=EOF){
        rep(i,1,n)
        scanf("%d%d",&num[i],&a[i]);
        init(dp,0);
        ans=0;
        dp[0]=1;
        rep(i,1,n) Multiply(num[i],a[i]);
        printf("%d\n",ans);
    }
    return 0;
}

还有一种优化多重背包的操作--多重背包的可行化问题(O(n*m)):

就是需要开一个数组记录凑出当期需要的个数,不能超过本身的个数,然后每次都需要初始化

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const int MOD=1e6;
const int INF= 0x3f3f3f3f;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define init(a,b) memset(a,b,sizeof a)
int dp[N<<1];
int a[200];
int num[200];
int w[N];
   int n,m;
   int ans;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while(scanf("%d%d",&m,&n)!=EOF){
        rep(i,1,n)
        scanf("%d%d",&num[i],&a[i]);//w[i]=a[i];
        init(dp,0);
        ans=0;
//        dp[0]=1;
        for(int i=1;i<=n;i++){
            init(w,0);
            for(int j=a[i];j<=m;j++){
                if(dp[j]<dp[j-a[i]]+a[i]&&w[j-a[i]]<num[i]){
                    dp[j]=dp[j-a[i]]+a[i];
                    w[j]=w[j-a[i]]+1;
                }
            }
        }
        printf("%d\n",dp[m]);
    }
    return 0;
}
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const int MOD=1e6;
const int INF= 0x3f3f3f3f;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define init(a,b) memset(a,b,sizeof a)
int dp[N<<1];
int a[200];
int num[200];
int w[N];
   int n,m;
   int ans;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while(scanf("%d%d",&m,&n)!=EOF){
        rep(i,1,n)
        scanf("%d%d",&num[i],&a[i]);//w[i]=a[i];
        init(dp,0);
        ans=0;
        dp[0]=1;
        for(int i=1;i<=n;i++){
            init(w,0);
            for(int j=a[i];j<=m;j++){
                if(!dp[j]&&dp[j-a[i]]&&w[j-a[i]]<num[i]){
                    ans=max(ans,j);
                    dp[j]=1;
                    w[j]=w[j-a[i]]+1;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

参考单调队列优化poj1276:https://blog.csdn.net/cokomowang/article/details/38897209

单调队列实现:https://blog.csdn.net/flyinghearts/article/details/5898183      https://www.cnblogs.com/JoeFan/p/4165956.html

参考文档:https://wenku.baidu.com/view/8ab3daef5ef7ba0d4a733b25.html

#include <iostream>
#include <algorithm>
#include<cstdio>
using namespace std;
 
#define MAX 35
 
const int MAX_V = 100004;
 
//v、n、w:当前所处理的这类物品的体积、个数、价值
//V:背包体积, MAX_V:背包的体积上限值
//f[i]:体积为i的背包装前几种物品,能达到的价值上限。
 
int total;
 void pack(int f[], int V, int v, int n, int w){
    
 
    int va[MAX_V], vb[MAX_V];   //va/vb: 主/辅助队列
    for (int j = 0; j < v; ++j){     //多重背包
        int *pb = va, *pe = va - 1;     //pb/pe分别指向队列首/末元素
        int *qb = vb, *qe = vb - 1;     //qb/qe分别指向辅助队列首/末元素
        for (int k = j, i = 0; k <= V; k += v, ++i) {
            if (pe  == pb + n){       //若队列大小达到指定值,第一个元素X出队。
                if (*pb == *qb) ++qb;   //若辅助队列第一个元素等于X,该元素也出队。
                    ++pb;
            }
            int tt = f[k] - i * w;
            *++pe = tt;                  //元素X进队
            //删除辅助队列所有小于X的元素,qb到qe单调递减,也可以用二分法
            while (qe >= qb && *qe < tt) --qe;
            *++qe = tt;              //元素X也存放入辅助队列
            f[k] = *qb + i * w;      //辅助队列首元素恒为指定队列所有元素的最大值
            //total = f[k];
         }
     }
}
 
int main()
{
    int V;
    while(~scanf("%d",&V)){
        int v[100010] = {0};
        int w[100010] = {0};
        int f[100010] = {0};
        int k;
        scanf("%d",&k);
        if(k == 0){
            cout<<"0"<<endl;
            continue;
        }
        for(int j = 0 ; j < k ; ++j ){
            scanf("%d%d",&v[j],&w[j]);
        }
        for(int j = 0 ; j < k ;++j){
            pack(f,V,w[j],v[j],w[j]);
        }
        for(int j = V ; j >=0 ; --j){//最大值f[V]开始搜索等于或小于限度的最大值
            if(f[j] <= V){
                cout<<f[j]<<endl;
                break;
            }
        }
        //或者直接cout<<f[V]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/83689896