BZOJ 2073: [POI2004]PRZ [DP 状压]

本文介绍了一种枚举子集的技巧,并通过一道具体题目展示了如何利用该技巧解决实际问题。文章提供了完整的C++代码实现,包括输入输出、状态转移等关键步骤。

传送门

水题不解释

这道题的主要目的在于记录一个枚举子集的技巧

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=18,S=(1<<16)+5,INF=1e9;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int m,n,a[N],w[N];
int f[S];
int main(){
    //freopen("in","r",stdin);
    m=read();n=read();
    for(int i=0;i<n;i++){
        a[i]=read(),w[i]=read();
        f[1<<i]=a[i];
    }
    int All=1<<n;
    memset(f,0x3f,sizeof(f));
    f[0]=0;
    for(int s=0;s<All;s++){
        int sum=0,slow=0;
        for(int i=0;i<n;i++) 
            if((1<<i)&s) sum+=w[i],slow=max(slow,a[i]);
        if(sum<=m) f[s]=slow;
        else for(int t=s&(s-1);t;t=s&(t-1)) f[s]=min(f[s],f[t]+f[s^t]);
    }
    printf("%d",f[All-1]);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值