您的位置:首页 > 其它

codevs 2218 [CTSC1999] 补丁vs错误 dfs+二进制

2017-10-23 21:20 260 查看
题目:

http://codevs.cn/problem/2218/

暴搜+剪枝;

x & ~ ( 1 << i ) : 将集合x中的元素i去除;

( x & i ) == i , 判断i是否为x的子集 ;

教训:

x=x&~ss[i].s4;
x=x|ss[i].s3;
dfs(tot+ss[i].tim,x);
x=x|ss[i].s4;//可能把没减的也加上了;
x=x&~ss[i].s3;


考虑用中间变量来解决;

(感谢loi_wzhd大佬 orz);

#include<iostr
4000
eam>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

const int MAXN=101;
int n,m,ans=2147483647;
bool vis[400001];
struct hh
{
int tim,s1,s2,s3,s4;
}ma[MAXN];

void dfs(int tot,int x)
{
if(tot>=ans || vis[x]) return;//不加T五组,加了跑44ms;
if(!x) {ans=tot;return;}

vis[x]=1;
for(int i=1;i<=m;i++)
{
int v;
if( (ma[i].s1 & x)==ma[i].s1  &&  !(ma[i].s2 & x))
{
v=x&~ma[i].s4,v|=ma[i].s3;//需要一个中间变量;
dfs(tot+ma[i].tim,v);
}
}

vis[x]=0;
return;
}

void solve()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
string s1,s2;
scanf("%d",&ma[i].tim);
cin>>s1>>s2;
for(int j=0;j<n;j++)
{
if(s1[j]=='+') ma[i].s1^=1<<j;
else if(s1[j]=='-') ma[i].s2^=1<<j;
}
for(int j=0;j<n;j++)
{
if(s2[j]=='+') ma[i].s3^=1<<j;
else if(s2[j]=='-') ma[i].s4^=1<<j;
}
}

dfs(0,(1<<n)-1);
cout<<(ans==2147483647?0:ans);
return;
}

int main()
{
solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: