「BalticOI 2011」Switch the Lamp On
Casper is designing an electronic circuit on a \(N \times M\) rectangular grid plate. There are \(N \times M\) square tiles that are aligned to the grid on the plate. Two (out of four) opposite corners of each tile are connected by a wire.
A power source is connected to the top left corner of the plate. A lamp is connected to the bottom right corner of the plate. The lamp is on only if there is a path of wires connecting power source to lamp. In order to switch the lamp on, any number of tiles can be turned by 90° (in both directions).

In the picture above the lamp is off. If any one of the tiles in the second column from the right is turned by 90° , power source and lamp get connected, and the lamp is on.
Write a program to find out the minimal number of tiles that have to be turned by 90° to switch the lamp on.
有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会。
有 \(N\times M\) 个这样的元件,你想将其排列成 \(N\) 行 \(M\) 列放在电路板上。电路板的左上角连接电源,右下角连接灯泡。
试求:至少要旋转多少个正方形元件才能让电源与灯泡连通。 \(1 \le N,M \le 500\)。
原电路连边权为 0 的边,反向对角线连边权为 1 的边,求最短路。
暴力解法:Dijkstra 堆优化,堆要用手写堆或 STL 手动堆。
正解:边权仅为 0 或 1 的图,显然用 deque 广搜,边权为 0 的 push_front,边权为 1 的 push_back。
以下是 暴力解法。正解不会写……
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n, m, d[260005];
char str[505]; bool v[260005];
int head[260005], nex[260005<<2], to[260005<<2], w[260005<<2];
struct node {
int t, d;
bool operator < (const node& A) const {return d>A.d; }
};
node q[260005<<2]; int qt;
inline int id(const int& x, const int& y) {
return (x-1)*(m+1)+y;
}
inline void add(const int& x, const int& y, const int& z) {
nex[++head[0]]=head[x], head[x]=head[0], to[head[0]]=y, w[head[0]]=z;
nex[++head[0]]=head[y], head[y]=head[0], to[head[0]]=x, w[head[0]]=z;
}
int main() {
scanf("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf("%s", str+1);
for (int j=1; j<=m; ++j) {
if (str[j]=='/') add(id(i, j+1), id(i+1, j), 0), add(id(i, j), id(i+1, j+1), 1);
else add(id(i, j), id(i+1, j+1), 0), add(id(i, j+1), id(i+1, j), 1);
}
}
if ((n+m)&1) {printf("NO SOLUTION\n"); return 0; }
memset(d, 0x3f, sizeof d);
q[++qt]=(node) {id(1,1), d[id(1,1)]=0}; push_heap(q+1, q+qt+1);
while (qt) {
register node now=q[1]; pop_heap(q+1, q+qt+1), --qt;
if (v[now.t]) continue; v[now.t]=true;
for (int i=head[now.t]; i; i=nex[i]) if (d[now.t] + w[i] < d[to[i]]) {
d[to[i]]= d[now.t]+ w[i], q[++qt]=(node) {to[i], d[to[i]]}, push_heap(q+1, q+qt+1);
}
}
printf("%d\n", d[id(n+1, m+1)]);
return 0;
}
「BalticOI 2011」Switch the Lamp On的更多相关文章
- 「CTSC 2011」排列
「CTSC 2011」排列 要求不存在公差为 A 或者公比为 B 的子列,那么实际上可以把该问题转化为求一个图的最优拓朴序. 任意差为 A 或者比为 B 的两个数连一条边. 求一个合法序列的答案可以用 ...
- 「CTSC 2011」幸福路径
[「CTSC 2011」幸福路径 蚂蚁是可以无限走下去的,但是题目对于精度是有限定的,只要满足精度就行了. \({(1-1e-6)}^{2^{25}}=2.6e-15\) 考虑使用倍增的思想. 定义\ ...
- LOJ#2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On
题目描述 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会.有 N×M 个这样 ...
- 「BZOJ 2342」「SHOI 2011」双倍回文「Manacher」
题意 记\(s_R\)为\(s\)翻转后的串,求一个串最长的形如\(ss_Rss_R\)的子串长度 题解 这有一个复杂度明显\(O(n)\)的做法,思路来自网上某篇博客 一个双倍回文串肯定当且仅当本身 ...
- 「BZOJ 2434」「NOI 2011」阿狸的打字机「AC自动机」
题意 有一个打字机,支持三种操作: 字符串末尾加一个小写字母 字符串末尾减一个字符 输出这个字符串 经过不超过\(n\)次操作后有\(m\)组询问:\((x,y)\),表示第\(x\)次输出第字符串在 ...
- loj 2778「BalticOI 2018」基因工程
loj luogu 这题和NOI那道向量内积一个套路 首先考虑求两行的不同元素个数,可以转化成一个行向量\(a\)和列向量\(b\)相乘得到一个值.如果只有\(A,C\)两种字符,那么令对应权值\(A ...
- 「BalticOI 2020」病毒
AC自动机+DP最短路转移 怎么说呢,挺套路的,也不是太难,但是一上手会被大量的信息淹没思路,还是要注意关注主要信息,不要被一些细节卡住 由于抗体是要在基因序里面出现过,那么考虑把抗体的序列检出AC自 ...
- Solution -「POI 2011」「洛谷 P3527」MET-Meteors
\(\mathcal{Description}\) Link. 给定一个大小为 \(n\) 的环,每个结点有一个所属国家.\(k\) 次事件,每次对 \([l,r]\) 区间上的每个点点权加上 ...
- PHP丨PHP基础知识之条件语SWITCH判断「理论篇」
Switch在一些计算机语言中是保留字,其作用大多情况下是进行判断选择.以PHP来说,switch(开关语句)常和case break default一起使用 典型结构 switch($control ...
随机推荐
- MVC中的cshtml与ASPX的区别
在MVC3中,即可以使用cshtml,也可以使用aspx, 这两者到底有什么区别呢? 越详细越好,如果是用来正式开发,用哪种比较好. --------------------------------- ...
- 网站私有CA证书制作
所谓的网站安全证书 是通过在客户端浏览器和Web服务器之间建立一条SSL安全通道保证了双方传递信息的安全性,而且用户可以通过服务器证书验证他所访问的网站是否真实可靠. 大体步骤: ...
- springboot工程启动时,报错:No bean named 'shiroFilter' available
在启动Springboot项目时,报错:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ' ...
- P3588 [POI2015]PUS(拓扑排序+线段树)
P3588 [POI2015]PUS 对于每个$(l,r,k)$,将$k$个位置向剩下$r-l-k+1$个位置连边,边权为$1$,这样就保证$k$个位置比剩下的大 先给所有位置填$1e9$保证最优 然 ...
- 关于“如何只用2GB内存从20亿,40亿,80亿个整数中找到出现次数最多的数?”的一种思路
小弟不才,只懂一些c#的皮毛,有一些想法, int32值范围大概在-20亿——20亿,按hashtable一个keyvalue占8B的设定来说,最大可以存储大约2.5亿个 数字-次数对. 那么,可以将 ...
- 继续死磕python
一.数据运算 算术运算 比较运算 赋值运算 逻辑运算 成员运算 身份运算 位运算 其中左右移运算是逻辑左右移即缺失位补0,而算数右移缺失补符号位(注意逻辑运算都是补码运算即都取补码再运算,然后结果也是 ...
- 统计学习方法——第四章朴素贝叶斯及c++实现
1.名词解释 贝叶斯定理,自己看书,没啥说的,翻译成人话就是,条件A下的bi出现的概率等于A和bi一起出现的概率除以A出现的概率. 记忆方式就是变后验概率为先验概率,或者说,将条件与结果转换. 先验概 ...
- ELK集群搭建
基于5台虚拟机,搭建ELK集群. 方案: 1. ELK是日志分析平台,而不是一款软件,是一整套解决方案,是三个软件产品的首字母缩写,ELK分别代表: Elasticsearch:负责日志检索和储存 L ...
- 2019 计蒜之道 复赛 E. 撑起信息安全“保护伞” (贪心,构造,规律)
为了给全球小学员打起信息安全"保护伞",VIPKID 还建立了一套立体化的安全防御体系,7 \times 247×24 小时持续安全监控与应急响应等多项联动,具备业界最高级别的数据 ...
- jupyter notebook添加环境
列出当前kernel: jupyter kernelspec list 删除已有环境:jupyter kernelspec remove NAME 安装新kernel ipython kernel i ...