Description

2255是一个傻X,他连自己家灯不亮了都不知道。

某天TZ大神路过他家,发现了这一情况,

于是TZ开始行侠仗义了。

TZ发现是电路板的问题,

他打开了电路板,发现线路根本没有连上!!

于是他强大的脑力可以使某个格子上的线路从\变为/,

或者从/变为\。

2255不会电路(因为他什么都不会),但是他想知道TZ最少要用多少次脑力才能使他家的灯变亮。

如果无法变亮,输出“NO SOLUTION”。

n,m<=500

Input

Output

Sample Input

3 5

\\/\\

\\///

/\\\\

Sample Output

1

……我是弱渣……原来以为是各种流乱搞,结果ksy学长告诉我是最短路

如果符号是“/”左下方的点向右上方的点连边权为0的边,左上方向右下方连边权为1的边

如果符号是“\”左上方的点向右下方的点连边权为0的边,左下方向右上方连边权为1的边

然后上最短路……别忘了加双向边

25w的点Dj+堆可以秒过,但是SPFA呵呵呵……不过我有特殊的优化SPFA的方法

要用SLF优化,就是更新的时候如果当前更新的dist比正在做的dist小,就把它放在队头。具体看代码,还不会的自行百度

#include<iostream>
#include<cstdio>
#include<cstring>
#define inf 127/3
#define mod 1000007
using namespace std;
struct edge{
int next,to,v;
}e[3000010];
int cnt;
int head[260000];
inline void ins(int u,int v,int w)
{
e[++cnt].to=v;
e[cnt].v=w;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void insert(int u,int v,int w)
{
ins(u,v,w);
ins(v,u,w);
}
int n,m,t,w=1;
char ch;
int dist[260000];
bool flag[260000];
int q[mod];
inline void spfa()
{
memset(dist,inf,sizeof(dist));
int now;q[1]=1;dist[1]=0;flag[1]=1;
while (t!=w)
{
t=(t+1)%mod;
now=q[t];
for (int i=head[now];i;i=e[i].next)
if (dist[now]+e[i].v<dist[e[i].to])
{
dist[e[i].to]=dist[now]+e[i].v;
if (!flag[e[i].to])
{
if(dist[now]>=dist[e[i].to])
{
q[t]=e[i].to;
flag[e[i].to]=1;
t=(t-1+mod)%mod;
}else
{
w=(w+1)%mod;
q[w]=e[i].to;
flag[e[i].to]=1;
}
}
}
flag[now]=0;
}
}
int main()
{
scanf("%d%d",&n,&m);
if ((n+m)&1)
{
printf("NO SOLUTION");
return 0;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
ch=' ';
while (ch!='/' && ch!='\\' ) scanf("%c",&ch);
if (ch=='\\')
{
insert((i-1)*(m+1)+j+1,i*(m+1)+j,1);
insert((i-1)*(m+1)+j,i*(m+1)+j+1,0);
}
else if (ch=='/')
{
insert((i-1)*(m+1)+j,i*(m+1)+j+1,1);
insert((i-1)*(m+1)+j+1,i*(m+1)+j,0);
}
}
spfa();
printf("%d\n",dist[(n+1)*(m+1)]);
}

最新文章

  1. 为什么DOM操作很慢
  2. Spark的WorkCount的例子
  3. AOP的实现机制
  4. sky
  5. hdu 3966 Aragorn&#39;s Story 树链剖分 按点
  6. 使用Python来对MySQL数据库进行操作
  7. 【液晶模块系列基础视频】5.4.X-GUI字体驱动4
  8. SQL查询(一)
  9. SharePoint表单和工作流 - Nintex篇(五)
  10. 1分钟去除word文档编辑限制密码
  11. Python 网页爬虫
  12. BZOJ1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛
  13. Java环境变量详解
  14. 常见类 Object
  15. 《http权威指南》读书笔记11
  16. 基于SpringMVC+Spring+MyBatis实现秒杀系统【概况】
  17. PHP读取txt文件到数组
  18. php+C#.net混合开发
  19. DOS批处理前言
  20. Windows下用HackRF和SDR#收听FM

热门文章

  1. 数组字符串与指针字符串的区别 char s[]=&quot;***&quot; 和char *s=&quot;***&quot;的区别
  2. MongoDB Connector for Hadoop
  3. [Protractor] Getting Started With Protractor
  4. USB通讯协议之深入理解
  5. this关键字之一个有趣的用法
  6. 获取scrollTop兼容各浏览器的方法,以及body和documentElement
  7. 使用VS Code开发AngularJS 2 第一个应用程序
  8. ArcEngine - 栅格数据访问的-对象模型
  9. js_day2
  10. PM2.5空气质量指数(AQI)是如何计算的