1. 程式人生 > >POJ 1276 多重揹包+多重揹包可行化問題+單調佇列優化

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;
}