「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 ...
随机推荐
- ssh-config的使用
使用SSH的配置文件可以在很大程度上方便各种操作,特别适应于有多个SSH帐号.使用非标准端口或者写脚本等情况. man ssh_config 可以查看手册 如果之前是用密码方式来登录SSH,需要先改用 ...
- Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- r子集代码实现(递归)
#!/usr/bin/env python #coding:utf-8 SET_START = 1 SET_END = 9 SUB_LEN = 10 def r_subset(i, r, pre, a ...
- vue视图不更新情况详解
vue视图不更新情况详解:https://www.jb51.net/article/161371.htm
- spring循环依赖是怎么解决的?
回答:循环依赖就是循环引用,就是两个或多个Bean相互之间的持有对方,比如CircleA引用CircleB,CircleB引用CircleA,则它们最终反映为一个环. Spring如何解决循环依赖? ...
- win 10 自带 Ubuntu 系统的文件位置
win 10 自带 Ubuntu 系统的文件位置 Ubuntu 作为最为流行 Linux 系统中的一种,是用来学习 Linux 相关知识是最好不过的选择.专门搞一个 Ubuntu 系统的电脑不太现实, ...
- 执行npm publish 报错:403 Forbidden - PUT https://registry.npmjs.org/kunmomotest - you must verify your email before publishing a new package: https://www.npmjs.com/email-edit
前言 执行npm publish 报错:403 Forbidden - PUT https://registry.npmjs.org/kunmomotest - you must verify you ...
- k3 cloud中如何把一个账套中的单据部署到另一个账套中
打开bos,依次点击->解决方案->部署包管理 填写部署包名称并点击下一步 选择需要部署的单据并点击下一步 确定后点击下一步: 点击完成 找到部署路径会成一个部署包: 部署:打开部署包安装 ...
- iOS app被拒整理
作者:Leon链接:http://www.zhihu.com/question/33191327/answer/71421736来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Centos安装 Apache2.4提示 APR not found的解决办法
在安装apache2.2.22版本的时候没有任何问题,可直接使用命令编译安装. 但是,在apache 2.4.12版本,./configure 进行配置时, 提示 configure: error: ...