01背包练习

hdu-2159 FATE

d[i][j]表示在有i容忍度和可杀j个怪的情况下,能够获得的最多的经验

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define DEBUG
const int maxn=100+5,maxv=26,INF=0x3f3f3f3f;
int n,m,K,s;
int a[maxn],b[maxn],d[maxn][maxn],c[maxn];
int main(){
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    while(scanf("%d%d%d%d",&n,&m,&K,&s)!=EOF){
        memset(d,0,sizeof(d));
        bool flag=0;
        for(int i=0;icin>>a[i]>>b[i];
        for(int i=1;i<=m;i++){
            for(int j=0;j//这一段
                for(int k=1;k<=s;k++)                            //其实是
                    if(b[j]<=i)                                  //对某一容忍度下的
                        d[i][k]=max(d[i][k],d[i-b[j]][k-1]+a[j]);//完全背包
            cout<"  "<if(d[i][s]>=n){
                cout<1;
                break;
            }
        }
        if(!flag)cout<<"-1"<#ifdef DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

HDU-2191

以前都不知道hdu这么垃圾,一个上午挂了两次
多重背包经典裸题,毫无优化,就是把k个相同的物品看成是k个独立的物品,分开来后用01背包模板来套

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define DEBUG
const int maxn=100+5,maxv=26,INF=0x3f3f3f3f;
int n,m;
int w[maxn],v[maxn],d[maxn],c[maxn];
int main(){
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t;
    cin>>t;
    while(t--){
        cin>>m>>n;
        for(int i=0;icin>>w[i]>>v[i]>>c[i];
        memset(d,0,sizeof(d));
        for(int i=0;ifor(int j=0;jfor(int k=m;k>=w[i];k--){
                    d[k]=max(d[k-w[i]]+v[i],d[k]);
                }
            }
        }
        printf("%d\n",d[m]);
    }
#ifdef DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

POJ-1384 Piggy-Bank

裸完全背包,求最小价值

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define DEBUG
const int maxn=500+5,maxv=26,INF=0x3f3f3f3f;
int n,m;
int w[maxn],v[maxn],d[10000+5];
int main(){
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t;
    cin>>t;
    while(t--){
        int t1,t2;
        cin>>t1>>t2;
        m=t2-t1;
        cin>>n;
        for(int i=0;icin>>v[i]>>w[i];
        memset(d,0x3f,sizeof(d));
        d[0]=0;
        for(int i=0;ifor(int j=w[i];j<=m;j++){
                d[j]=min(d[j-w[i]]+v[i],d[j]);
            }
        }
        if(d[m]==INF)printf("This is impossible.\n");
        else
        printf("The minimum amount of money in the piggy-bank is %d.\n",d[m]);
    }
#ifdef DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

HDU-1248 寒冰王座

裸完全背包,没有任何优化

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
//#define DEBUG
const int maxn=10000+5,maxv=26,INF=0x3f3f3f3f;
int n,m;
int w[maxn],v[maxn],d[maxn];
int main(){
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t;
    cin>>t;
    w[0]=150;
    w[1]=200;
    w[2]=350;
    n=3;
    while(t--){
        cin>>m;
        memset(d,0,sizeof(d));
        for(int i=0;ifor(int j=w[i];j<=m;j++){
                d[j]=max(d[j-w[i]]+w[i],d[j]);
            }
        }
        printf("%d\n",m-d[m]);
    }
#ifdef DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

HDU2602:Bone Collector

裸01背包

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define DEBUG
const int maxn=1000+5,maxv=26,INF=0x3f3f3f3f;
int n,m;
int w[maxn],v[maxn],d[maxn];
int main(){
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t;
    cin>>t;
    while(t--){
        memset(d,0,sizeof(d));
        cin>>n>>m;
        for (int i = 0; i < n; ++i)cin>>v[i];
        for (int i = 0; i < n; ++i)cin>>w[i];
        for (int i = 0; i < n; ++i)
        {
            for(int j=m;j>=w[i];j--){
                d[j]=max(d[j],d[j-w[i]]+v[i]);
            }
        }
        printf("%d\n",d[m]);
    }
#ifdef DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

HDU2546:饭卡

关键是先找出最大价格的物品,然后再dp求出保留5元后,可以花费最多的值,最后再减去最大价格

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define DEBUG
const int maxn=1000+5,maxv=26,INF=0x3f3f3f3f;
int n;
int w[maxn],m,d[maxn];
int main(){
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    while((scanf("%d",&n)!=EOF)&&n!=0){
        memset(d,0,sizeof(d));
        for(int i=0;icin>>w[i];
        }
        cin>>m;
        if(m<5){printf("%d\n",m);continue;}
        sort(w,w+n);
        for (int i = 0; i < n-1; ++i)
        {
            for (int j = m-5; j >= w[i] ; --j)
            {
                d[j]=max(d[j-w[i]]+w[i],d[j]);
            }
        }
        printf("%d\n",m-d[m-5]-w[n-1] );
    }
#ifdef DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

你可能感兴趣的:(01背包练习)