[USACO09NOV]灯Lights

1 篇文章 0 订阅
1 篇文章 0 订阅

题目描述

Bessie and the cows were playing games in the barn, but the power was reset and the lights were all turned off. Help the cows get all the lights back on so they can resume their games.
The N (1 <= N <= 35) lights conveniently numbered 1..N and their switches are arranged in a complex network with M (1 <= M <= 595) clever connection between pairs of lights (see below).
Each light has a switch that, when toggled, causes that light – and all of the lights that are connected to it – to change their states (from on to off, or off to on).
Find the minimum number of switches that need to be toggled in order to turn all the lights back on.
It’s guaranteed that there is at least one way to toggle the switches so all lights are back on.
贝希和她的闺密们在她们的牛棚中玩游戏。但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了。贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望。她希望您能够帮帮她,把所有的灯都给重新开起来!她才能继续快乐地跟她的闺密们继续玩游戏! 牛棚中一共有N(1 <= N <= 35)盏灯,编号为1到N。这些灯被置于一个非常複杂的网络之中。有M(1 <= M <= 595)条很神奇的无向边,每条边连接两盏灯。 每盏灯上面都带有一个开关。当按下某一盏灯的开关的时候,这盏灯本身,还有所有有边连向这盏灯的灯的状态都会被改变。状态改变指的是:当一盏灯是开著的时候,这盏灯被关掉;当一盏灯是关著的时候,这盏灯被打开。 问最少要按下多少个开关,才能把所有的灯都给重新打开。 数据保证至少有一种按开关的方案,使得所有的灯都被重新打开。

输入输出格式

输入格式:

Line 1: Two space-separated integers: N and M.
Lines 2..M+1: Each line contains two space-separated integers representing two lights that are connected. No pair will be repeated.

输出格式:

Line 1: A single integer representing the minimum number of switches that need to be flipped in order to turn on all the lights.

输入输出样例

输入样例#1:

5 6
1 2
1 3
4 2
3 4
2 5
5 3

输出样例#1:

3

说明

There are 5 lights. Lights 1, 4, and 5 are each connected to both lights 2 and 3.
Toggle the switches on lights 1, 4, and 5.


折半搜索(Meet in the Middle)

对于该问题,我们可以想到使用状态压缩进行搜索,时间复杂度为 O(2n) 。很明显这样无法在限定时间内解决问题。但是我们还有使用空间换取时间的方法,能够使 n 的规模降到n/2,观察此时达到的状态也是 2n/2 规模,恰好符合此题的要求。首先我们搜索从编号为1的灯到编号为 n/2 的灯,记录完成后点亮了哪些灯,以及达到该状态最小的点灯次数。再直接从 n/2+1 搜索到 n <script type="math/tex" id="MathJax-Element-7">n</script>,如果到最后有些灯没有被点亮,则可以查之前搜索中,是否有方案能够把剩下的灯都点亮。
在具体记录中间状态时,可以直接使用STL set, 用位压的方法足够用long long来记录每一盏灯的亮灭情况。

/*112ms , 9.82MB*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 40;
const int INF = 0x3f3f3f3f;
ll e[N], bin[N], ed;
map<ll, int> mp;
int n, m, mt = INF, cnt;
bool flag=0;
void dfs(int x, ll now, int used){
    if(x == cnt+1){
        if(now == ed) mt = min(mt, used);
        if(!flag){
            int t = mp[now];
            if(!t||t>used) mp[now] = used;
        }
        else{
            int t = mp[ed-now];
            if(!t) return;
            mt = min(mt, t+used);
        }
        return;
    }
    dfs(x+1, now, used);
    dfs(x+1, now^e[x], used+1);
}

int main()
{
    for(int i = 1; i < N; i++) bin[i] = (ll)1 << (i-1);
    scanf("%d%d", &n, &m);
    ed = bin[n+1]-1;
    for(int i = 1; i <= n; i++) e[i]+=bin[i];//用e[i]表示该灯与其他灯的联系,
                                            //与第i盏灯有关系,则第i-1位为1
    for(int i = 1; i <= m; i++){
        int u, v;
        scanf("%d%d", &u, &v);
        e[u]+=bin[v]; e[v]+=bin[u];
    }
    cnt = n/2; dfs(1, 0, 0);
    flag = 1;
    cnt = n;dfs(n/2+1, 0, 0);
    printf("%d\n", mt);
    return 0;
}

高斯消元解异或方程组+DFS

对于开关灯问题,我们很自然地想到高斯消元。当我们对原方程进行高斯消元后。我们发现如果该变量是非自由变元,则我们对着盏灯进行的操作就是确定的,可以通过之前的操作情况递推出来,否则该变量是自由变元,那么意味着我们有两种选择——操作或不操作。这时我们不如对该元进行深搜。

/*0ms , 2.12MB*/
#include<bits/stdc++.h>
using namespace std;
const int N = 40;
int f[N][N], x[N];
int n, m, ans = INT_MAX;
void Gauss()
{
    for(int i = 1; i <= n; i++){
        int j = i;
        while(j <= n && !f[j][i]) j++;
        if(j > n) continue;
        if(i != j) for(int k = 1; k <= n+1; k++) swap(f[i][k], f[j][k]);
        for(int j = i+1; j <= n; j++)
            if(f[j][i])
                for(int k = 1; k <= n+1; k++)
                    f[j][k]^=f[i][k];
    }
}
void dfs(int now, int cnt){
    if(cnt >= ans) return;
    if(!now){ans = min(ans, cnt); return;}
    if(f[now][now]){
        x[now] = f[now][n+1];//x数组保存当前的操作情况
        for(int i = now+1; i <=n; i++) x[now]^=x[i]&f[now][i];
        if(x[now]) dfs(now-1, cnt+1);
        else dfs(now-1, cnt);
    }
    else{
        x[now] = 0; dfs(now-1, cnt);
        x[now] = 1; dfs(now-1, cnt+1);
    }
}
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) f[i][i] = f[i][n+1] = 1;//邻接矩阵保存
    for(int i = 1; i <= m; i++){
        int u, v;
        scanf("%d%d", &u, &v);
        f[u][v] = f[v][u] = 1;
    }
    Gauss();
    dfs(n, 0);
    printf("%d\n", ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值