题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=2346

https://loj.ac/problem/2632

题解

普及组难度的题都要想十几分钟,没救了。

对于一条边,将原来就能连起来的两个点之间建一条长度为 \(0\) 的边,翻转以后能连起来的两个点建一条长度为 \(1\) 的边。然后跑最短路就可以了。因为边权只有 \(01\),可以直接 01bfs


代码如下:

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 500 + 7;
const int M = N * N;
const int INF = 0x3f3f3f3f; int n, m;
int q[M << 1], id[N][N], dis[M], vis[M];
char a[N][N]; struct Edge { int to, ne, w; } g[M << 2]; int head[M], tot;
inline void addedge(int x, int y, int z) { g[++tot].to = y, g[tot].w = z, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y, int z) { addedge(x, y, z), addedge(y, x, z); } inline void bfs() {
int hd = M, tl = M + 1;
memset(dis, 0x3f, sizeof(dis)), q[tl] = 1, dis[1] = 0;
while (hd < tl) {
int x = q[++hd];
if (vis[x]) continue;
vis[x] = 1;
if (x == id[n + 1][m + 1]) return;
for fec(i, x, y) if (!vis[y] && smin(dis[y], dis[x] + g[i].w))
if (g[i].w) q[++tl] = y;
else q[hd--] = y;
}
} inline void work() {
int tmp = 0;
for (int i = 1; i <= n + 1; ++i)
for (int j = 1; j <= m + 1; ++j) id[i][j] = ++tmp;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
adde(id[i][j], id[i + 1][j + 1], a[i][j] == '/');
adde(id[i + 1][j], id[i][j + 1], a[i][j] == '\\');
}
bfs();
if (dis[id[n + 1][m + 1]] != INF) printf("%d\n", dis[id[n + 1][m + 1]]);
else puts("NO SOLUTION");
} inline void init() {
read(n), read(m);
for (int i = 1; i <= n; ++i) scanf("%s", a[i] + 1);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj2346 & loj2632 [Baltic 2011]Lamp 最短路的更多相关文章

  1. BZOJ2346:[Baltic 2011]Lamp(最短路)

    Description 2255是一个傻X,他连自己家灯不亮了都不知道. 某天TZ大神路过他家,发现了这一情况, 于是TZ开始行侠仗义了. TZ发现是电路板的问题, 他打开了电路板,发现线路根本没有连 ...

  2. 【bzoj2346】[Baltic 2011]Lamp 堆优化Dijkstra

    题目描述 2255是一个傻X,他连自己家灯不亮了都不知道.某天TZ大神路过他家,发现了这一情况,于是TZ开始行侠仗义了.TZ发现是电路板的问题,他打开了电路板,发现线路根本没有连上!!于是他强大的脑力 ...

  3. Bzoj 2346: [Baltic 2011]Lamp dijkstra,堆

    2346: [Baltic 2011]Lamp Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 428  Solved: 179[Submit][Sta ...

  4. 【搜索 ex-BFS】bzoj2346: [Baltic 2011]Lamp

    关于图中边权非零即一的宽度优先搜索 Description 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组 ...

  5. bzoj2346[Baltic 2011]Lamp

    Description 2255是一个傻X,他连自己家灯不亮了都不知道. 某天TZ大神路过他家,发现了这一情况, 于是TZ开始行侠仗义了. TZ发现是电路板的问题, 他打开了电路板,发现线路根本没有连 ...

  6. [Baltic 2011]Lamp BZOJ2346

    分析: 建图最短路,比较裸. 我们可以考虑,如果是‘\’那么,左上连右下边权为0,左下连右上边权为1,反之亦然. 卡裸spfa,加点优化能过,我就直接改成的堆优化Dijkstra 附上代码: #inc ...

  7. 【刷题】BZOJ 2346 [Baltic 2011]Lamp

    Description 2255是一个傻X,他连自己家灯不亮了都不知道. 某天TZ大神路过他家,发现了这一情况, 于是TZ开始行侠仗义了. TZ发现是电路板的问题, 他打开了电路板,发现线路根本没有连 ...

  8. BZOJ 2346: [Baltic 2011]Lamp Dijkstra

    不难发现如果一个边的方向改变,就一定不会改回来(这样肯定不是最短路). 所以就直接建双向边,边权为 $0$ 代表不改变,边权为 $1$ 代表改变,跑一个最短路即可. #include <bits ...

  9. BZOJ2348: [Baltic 2011]Plagiarism

    2348: [Baltic 2011]Plagiarism Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 304  Solved: 141[Submit ...

随机推荐

  1. 【HDOJ6666】Quailty and CCPC(模拟)

    题意:给出罚时现场赛的题数和罚时,问是否有在金牌线下取整被卡出,四舍五入卡入的队伍 n<=1e5 思路: #include<bits/stdc++.h> using namespac ...

  2. 高级Javascript代码

    Javascript是一门很吊的语言,我可能学了假的JavaScript,哈哈,大家还有什么推荐的,补充送那啥邀请码. 本文秉承着:你看不懂是你SB,我写的代码就要牛逼. 1.单行写一个评级组件 &q ...

  3. JDBC连接sql server数据库的详细步骤和代码 转

    JDBC连接sql server数据库的步骤如下: 1.加载JDBC驱动程序(只做一次): 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.C ...

  4. mui初级入门教程(三)— html5+ XMLHttpRequest 与mui ajax用法详解

    文章来源:小青年原创发布时间:2016-05-29关键词:mui,html5+,XMLHttpRequest,ajax,懒加载转载需标注本文原始地址: http://zhaomenghuan.gith ...

  5. thread_process_action

    import math import random import re import sys import threading from time import ctime, sleep from l ...

  6. unittest测试框架生成可视化测试报告-BeautifulReport

    生成报告的样式: 在说unittest之前,先说几个概念: TestCase 也就是测试用例 TestSuite 多个测试用例集合在一起,就是TestSuite TestLoader是用来加载Test ...

  7. mariadb(二)增删改

    一.表的结构的增删改 添加数据类型 alter table 表名 add 字段 数据类型: #如果字段存在则会报错 Duplicate column name '字段' #修改数据类型 alter t ...

  8. BigDecimal保留小数处理

    最近在处理支付相关的需求,涉及到金额的问题,采用传统的基本数据类型处理会存在误差,因此采用BigDecimal对象进行处理. 一.构造BigDecimal对象的方式 BigDecimal(int)   ...

  9. HTML5-注册表单案例fieldset

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. IDF-CTF-图片里的英语 writeup

    题目链接:http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=34 一恒河沙中有三千世界,一张图里也可以有很多东西. ...