zoukankan      html  css  js  c++  java
  • 【Luogu】P2962灯Lights(折半搜索)

      题目链接

      本意是想学高斯消元,然后一顿乱搞之后学到了一个神奇的搜索方式叫做折半搜索。

      qwq

      就是我先dfs前二分之n个点,然后再dfs后二分之n个点。

      然后我dfs后二分之n个点的时候判断一下第一次dfs有没有搜到互补的状态(就是当前状态能不能跟之前搜到过的一个状态异或起来变成(1<<n)-1,即所有灯都开着)

      如果能就看看能不能更新答案。

      然后快的飞起qwq

      

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<map>
    #define maxn 37
    #define maxs (1<<14)
    using namespace std;
    map<long long,int> sta,stb;
    inline long long read(){
        long long num=0,f=1;
        char ch=getchar();
        while(!isdigit(ch)){
            if(ch=='-')    f=-1;
            ch=getchar();
        }
        while(isdigit(ch)){
            num=num*10+ch-'0';
            ch=getchar();
        }
        return num*f;
    }
    
    int n,m;
    long long chan[maxn];
    long long Max;
    long long cnt;
    int ans=0x7fffffff;
    
    void dfs(int x,long long state,int num){
        cnt++;
        if(sta.count(state))    sta[state]=min(sta[state],num);
        else                    sta[state]=num;
        if(x==(n>>1)+1)    return;
        long long now=state^chan[x];
        dfs(x+1,state,num);
        dfs(x+1,now,num+1);
    }
    
    void dfs2(int x,long long state,int num){
        cnt++;
        if(stb.count(state))    stb[state]=min(stb[state],num);
        else                    stb[state]=num;
        if(sta.count(Max^state))    ans=min(ans,sta[Max^state]+stb[state]);
        if(x==n+1)    return;
        long long now=state^chan[x];
        dfs2(x+1,state,num);
        dfs2(x+1,now,num+1);
    }
    
    int main(){
        n=read(),m=read();
        Max=(1LL<<n)-1;
        for(int i=1;i<=m;++i){
            long long x=read(),y=read();
            chan[x]|=(1LL<<(y-1));
            chan[y]|=(1LL<<(x-1));
        }
        for(long long i=1;i<=n;++i)    chan[i]|=(1LL<<(i-1));
        dfs(1,0,0);
        dfs2((n>>1)+1,0,0);
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    JBPM工作流(四)——管理流程定义
    JBPM工作流(三)——ProcessEngine与Service API
    JBPM工作流(二)——数据库表说明
    JBPM工作流(一)——实现一个简单的工作流例子
    jbpm与spring hibernate struts整合
    SpringMVC12拦截器
    SpringMVC11文件上传
    阅读代码的方法
    关于linux系统的资料
    关于图灵机的介绍(相见恨晚,太赞了)
  • 原文地址:https://www.cnblogs.com/cellular-automaton/p/8242580.html
Copyright © 2011-2022 走看看