UVA - 10817 状压DP

题意:大白P95
本题比较特别的是状压两个集合并且进行转移,因此要分别处理当前集合只有1个老师/2个老师的记录(然后可O(1)得出0个老师的集合)
记忆化过了但是迭代式不能记忆超过2的之前的状态是怎样的,除非多记录一个超过2的集合,因此无法实现(是我太菜了)
PS.偶然发现一个现象,当数组开的过大时效率会变得十分低下

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<sstream>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
using namespace std;
const int maxn = 1e5+11;
typedef long long ll;
int s,m,n,a[maxn],aa[maxn],b[maxn],bb[maxn];
bool vis[maxn];
ll dp[123][1<<8|1][1<<8|1];//m teachers S subjects 1person 2person
ll DP(int cur,int S1,int S2){
    if(cur>m+n)return S2==(1<<s)-1?0:1ll<<33;
    if(~dp[cur][S1][S2]) return dp[cur][S1][S2];
    int x=S1|b[cur];
    int all=(1<<s)-1;
    ll ans=1ll<<33;
    if(cur>m) ans=DP(cur+1,S1,S2);
    ans=min(ans,DP(cur+1,S1-(S1&b[cur])+(all&b[cur]),S2|(S1&b[cur]))+a[cur]);
    return dp[cur][S1][S2]=ans;
}
int main(){
    while(cin>>s>>m>>n){
        if(s==0) break;
        rep(i,1,m+n){
            scanf("%d",&a[i]);
            int t=0;char ch;
            while(ch=getchar()){
                if(ch>='0'&&ch<='9')t|=1<<(ch-'0'-1);
                else if(ch=='\n'||ch==EOF)break;
            }
            b[i]=t;
        }
        // rep(i,1,n+m)cout<<b[i]<<" ";cout<<endl;
        // memset(dp,0x3f,sizeof dp);
        // memset(dp[0],0,sizeof dp[0]);
        // for(int i=1;i<=m+n;i++){
        //     for(int S=0;S<=(1<<s)-1;S++){//two or one course
        //         for(int S0=S;S0;S0=(S0-1)&S){// one
        //             int one=S0,two=S^S0,zero=((1<<s)-1)^S;
        //             int x=zero&b[i],y=one&b[i],z=two&b[i];
        //             if(i<=m){
        //                 dp[i][one][two]=dp[i-1][zero&b[i]][y|two]+a[i];
        //             }else{
        //                 dp[i][one][two]=dp[i-1][zero&b[i]][y|two]+a[i];
        //                 dp[i][one][two]=min(dp[i][one][two],dp[i-1][one][two]);
        //             }
        //         }
        //     }
        // }
        // cout<<dp[n+m][0][(1<<s)-1]<<endl;
        memset(dp,-1,sizeof dp);
        cout<<DP(0,0,0)<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/caturra/p/8719301.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值