01背包入门

来源:互联网 发布:php网店 编辑:程序博客网 时间:2024/05/16 12:11

01背包问题模型:

N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。

这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。

用子问题定义状态:即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是:

f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}

hdu 2546

因为余额大于等于5元 就可以购买任何价格的物品 

所以我们可以预留5元来购买最贵的物品 其他的钱使用01背包 去得到最大的价值

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 10010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}int price[MAXN],dp[MAXN];int main(){//    fread;    int n,m;    while(scanf("%d",&n)!=EOF)    {        if(n==0) break;        MEM(dp,0);        for(int i=0;i<n;i++)            scanf("%d",price+i);        scanf("%d",&m);        if(m<5)        {            printf("%d\n",m);            continue;        }        sort(price,price+n);//        for(int i=0;i<n;i++)//            cout<<price[i]<<endl;        m-=5;//剩下的5元用来买最贵的        for(int i=0;i<n-1;i++)        {            for(int j=m;j>=price[i];j--)            {                dp[j]=max(dp[j],dp[j-price[i]]+price[i]);            }        }        printf("%d\n",m+5-dp[m]-price[n-1]);    }    return 0;}


hdu 2602

裸的01背包

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 1010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}int val[MAXN],vol[MAXN];int dp[MAXN];int main(){    //fread;    int tc;    scanf("%d",&tc);    while(tc--)    {        int n,v;        scanf("%d%d",&n,&v);        for(int i=0;i<n;i++)            scanf("%d",&val[i]);        for(int i=0;i<n;i++)            scanf("%d",&vol[i]);        MEM(dp,0);        for(int i=0;i<n;i++)        {            for(int j=v;j>=vol[i];j--)            {                dp[j]=max(dp[j],dp[j-vol[i]]+val[i]);            }        }        printf("%d\n",dp[v]);    }    return 0;}


hdu 2639

题意在上一题的基础上 增加了 求第k大

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 1010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}struct node{    int cost,val;}no[MAXN];int dp[MAXN][35];int a[MAXN],b[MAXN];int main(){//    fread;    int tc;    scanf("%d",&tc);    while(tc--)    {        int n,v,k;        scanf("%d%d%d",&n,&v,&k);        for(int i=0;i<n;i++)            scanf("%d",&no[i].val);        for(int i=0;i<n;i++)            scanf("%d",&no[i].cost);        MEM(dp,0);        for(int i=0;i<n;i++)        {            for(int j=v;j>=no[i].cost;j--)            {                for(int l=1;l<=k;l++)//将放与不放的结果分别存进两个数组                {                    a[l]=dp[j-no[i].cost][l]+no[i].val;                    b[l]=dp[j][l];                }                int x,y,z;                x=y=z=1;                a[k+1]=b[k+1]=-1;                while(z<=k&&(x<=k||y<=k))//循环找出第k优解                {                    if(a[x]>b[y])                    {                        dp[j][z]=a[x];                        x++;                    }                    else                    {                        dp[j][z]=b[y];                        y++;                    }                    if(dp[j][z]!=dp[j][z-1])                        z++;                }            }        }        printf("%d\n",dp[v][k]);    }    return 0;}

hdu 2955

有一个可以接受的被抓概率P 有n家银行可以去盗窃

下面分别给出每家银行可以盗窃的钱和被抓的概率

因为只有不被抓盗窃的钱才会算 所以我们需要将被抓的概率p 转化为不被抓的概率1-p

使用01背包的想法 去计算盗窃相应的银行不被抓的概率

如果概率大于1-p则是可行的 

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 10010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}struct node{    double p;    int money;}no[MAXN];double dp[MAXN];int main(){//    fread;    int tc;    scanf("%d",&tc);    while(tc--)    {        double p;        int n;        scanf("%lf%d",&p,&n);        p=1.0-p;        int sum=0;        for(int i=0;i<n;i++)        {            scanf("%d%lf",&no[i].money,&no[i].p);            sum+=no[i].money;            no[i].p=1.0-no[i].p;        }        MEM(dp,0);        dp[0]=1.0;        for(int i=0;i<n;i++)        {            for(int j=sum;j>=no[i].money;j--)            {                dp[j]=max(dp[j],dp[j-no[i].money]*no[i].p);            }        }        for(int i=sum;i>=0;i--)        {            if(dp[i]-p>eps)            {                printf("%d\n",i);                break;            }        }    }    return 0;}


hdu 3466

如果手上的钱少于Qi 则不能进行购买

另外 我们考虑 如果现在有两个物品 相应的p1,q1,p2,q2

如果先买A后买B 则要求至少有p1+q2 如果先买B后买A 则要求至少有p2+q1

如果在先买A的情况下较优 则p1+q2<p2+q1 即q1-p1>q2-p2

所以需要按q-p的值由大到小排序  这样解决后效性的问题

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 10010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}struct node{    int p,q,v;    bool operator <(const node &x)const    {        return q-p<x.q-x.p;    }}no[550];int dp[5050];int main(){//    fread;    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(int i=0;i<n;i++)            scanf("%d%d%d",&no[i].p,&no[i].q,&no[i].v);        MEM(dp,0);        sort(no,no+n);        for(int i=0;i<n;i++)        {            for(int j=m;j>=no[i].q;j--)            {                dp[j]=max(dp[j],dp[j-no[i].p]+no[i].v);            }        }        printf("%d\n",dp[m]);    }    return 0;}




hdu 1864

给出报销额度 n张发票

每张发票只能报销ABC三类  每类的不能超过600元  每张总的不能超过1000元


先处理出 所有发票中能够报销的金额  剩下的就是用01背包的思想求出最大报销金额

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 3000010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}int dp[MAXN];int money[40];int main(){//    fread;    double q;    int n;    while(scanf("%lf%d",&q,&n)!=EOF)    {        if(n==0) break;        int sum=(int)(q*100);        int num=0;        for(int i=0;i<n;i++)        {            int m;            scanf("%d",&m);            int a,b,c;            a=b=c=0;            int flag=1;            while(m--)            {                char ch; double x;                scanf(" %c:%lf",&ch,&x);                int y=(int)(x*100);                if(ch=='A'&&a+y<=60000)                    a+=y;                else if(ch=='B'&&b+y<=60000)                    b+=y;                else if(ch=='C'&&c+y<=60000)                    c+=y;                else                    flag=0;            }            if(flag&&a<=60000&&b<=60000&&c<=60000&&(a+b+c)<=100000)                money[num++]=a+b+c;        }        MEM(dp,0);        for(int i=0;i<num;i++)        {            for(int j=sum;j>=money[i];j--)            {                dp[j]=max(dp[j],dp[j-money[i]]+money[i]);            }        }        double ans=dp[sum]*1.0/100.0;        printf("%.2lf\n",ans);    }    return 0;}





0 0