Luogu_ P2962 [USACO09NOV] 灯 【题解】 双向搜索

题面:https://www.luogu.org/problem/P2962

标签有高斯消元。

但是并没有发现怎么用。

于是搜索。

2的35次方必然不可以接受。

双向搜索。

但是这个题不是从起始和终点搜。

而是起始和中点   有点怪

这样就可以降低复杂度了。

代码和注释如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll f[40],ed,b[40],cnt;
int n,m,minn=0x7fffffff;
bool h;
map<ll,int> mp;  //表示到某状态需要的步数 
void dfs(int x,ll now,int step){//x 为第几个开关  now为现在的状态  step 为步数
    if(x==cnt+1){
        if(now==ed)
            minn=min(minn,step);
        else{
            if(!h){//前一半
                int t=mp[now];
                if(t==0||step<t) mp[now]=step;
            }else{//后一半更新要有另外的一种,就是要和前一半结合
                int t=mp[ed-now];
                if(t!=0) minn=min(minn,t+step);
            }
        }
        return;
    }
    dfs(x+1,now^f[x],step+1);
    dfs(x+1,now,step);
}
int main()
{
    scanf("%d%d",&n,&m);
    f[1]=b[1]=1;
    for(int i=2;i<=n+1;i++) f[i]=b[i]=(b[i-1]<<1);
    ed=b[n+1]-1;//处理结尾
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        f[u]+=b[v];f[v]+=b[u];
    }
    h=0;cnt=n/2;dfs(1,0,0);
    h=1;cnt=n;dfs(n/2+1,0,0);
    printf("%d\n",minn);
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ChrisKKK/p/11458859.html