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;
}
上一篇:【LeetCode】1004. Max Consecutive Ones III 解题报告(C++)


下一篇:bootstrap下jQuery自动完成的样式调整-【jQuery】