Ivan's Car

Time limit: 1.5 second
Memory limit: 64 MB
The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are still useful, but even they became too steep because of soil movements.
Fortunately, engineer Ivan has a car, which can go well uphill and downhill. But there are different gear-modes for movement up and down, so during the driving you have to change gear-modes all the time. Also engineer Ivan has a good friend –– geologist Orlov. Together they are able to invent a plan for world saving. But, unfortunately, geologist Orlov lives in another town.
Ivan wants to save the world, but gear-box in his car started to wear out, so he doesn’t know, how long he will be able to use it. Please help Ivan to save the world. Find a route to the Orlov's town, such that Ivan will have to change gear-modes as few times as possible. In the beginning of the way Ivan can turn on any of gear-modes and you don't have to count this action as a changing of gear-mode.

Input

There are two positive integer numbers n and m in the first line, the number of towns and roads between them respectively (2 ≤ n ≤ 10 000; 1 ≤ m ≤ 100 000). Next m lines contain two numbers each — numbers of towns, which are connected by road. Moreover, the first is the town, which is situated below, from which you should go uphill by this road. Every road can be used for traveling in any of two directions. There is at most one road between any two cities. In the last line there are numbers of two cities, in which Ivan and geologist Orlov live, respectively. Although the majority of roads were destroyed, Ivan knows exactly, that the way to geologist Orlov's city exists.

Output

Output the smallest number of gear-modes changes on the way to Orlov's city.

Samples

input output
3 2
1 2
3 2
1 3
1
3 3
1 2
2 3
3 1
1 3
0
Problem Author: Grigoriy Nazarov (prepared by Bulat Zaynullin)
【分析】题意就不说了,应该都能看懂,我说说我的做法,可能有点繁琐。
 先用vector存边,up存向上的,down存向下的,然后BFS,用优先队列存状态,每次都从队列中取操作数最小的,由于复杂度跟内存问题,需要减枝,当走到一个点,看当前的操作数跟上一次的大小,如果小则放入队列,如果相同则看这个点上次的方向与这一次是否相同,如果不相同且这个点的这个方向也没有出现过,则放入队列。
一开始一直错,改来改去到了Text 11,先是MLE,改了一下,WA,再改 TLE,都快放弃了,后来加了个vis,过了,以后遇到这种情况千万不能放弃。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N=;
const int M=;
vector<int>up[N],down[N];
int n,m;
int a,b;
int step[N],lastdir[N];
bool vis[N][];
struct man {
int sum;
int dir;
int num;
bool operator<(man aa)const {
return sum>aa.sum;
}
};
priority_queue<man>q;
void init(){
for(int i=; i<up[a].size(); i++) {
man s;
s.sum=;
s.num=up[a][i];
s.dir=;q.push(s);vis[a][]=true;
}
for(int i=; i<down[a].size(); i++) {
man s;
s.sum=;
s.num=down[a][i];
s.dir=;q.push(s);vis[a][]=true;
}
}
void bfs() {
init();
memset(step,inf,sizeof(step));
step[a]=;
while(!q.empty()){
man t=q.top();q.pop();
int tt=t.num;//printf("!!!dir=%d num=%d sum=%d\n",t.dir,t.num,t.sum);
if(tt==b){
printf("%d\n",t.sum);
return;
}
for(int i=;i<up[tt].size();i++){
man k=t;k.num=up[tt][i];
if(t.dir==)k.dir=,k.sum++;
if(step[k.num]>k.sum){
step[k.num]=k.sum;lastdir[k.num]=k.dir;
q.push(k);
}
else if(step[k.num]==k.sum&&lastdir[k.num]!=k.dir&&!vis[k.num][k.dir]){
lastdir[k.num]=k.dir;
q.push(k);
}
}
for(int i=;i<down[tt].size();i++){
man k=t;k.num=down[tt][i];
if(t.dir==)k.dir=,k.sum++;
if(step[k.num]>k.sum){
step[k.num]=k.sum;lastdir[k.num]=k.dir;
q.push(k);
}
else if(step[k.num]==k.sum&&lastdir[k.num]!=k.dir&&!vis[k.num][k.dir]){
lastdir[k.num]=k.dir;
q.push(k);
}
}
}
}
int main() {
memset(vis,false,sizeof(vis));
scanf("%d%d",&n,&m);
while(m--) {
scanf("%d%d",&a,&b);
up[a].push_back(b);
down[b].push_back(a);
}
scanf("%d%d",&a,&b);
bfs();
return ;
}

URAL 1930 Ivan's Car(BFS)的更多相关文章

  1. 1930. Ivan's Car(spfa)

    1930 简单二维 标记一下是上坡还是下坡 #include <iostream> #include<cstdio> #include<cstring> #incl ...

  2. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  3. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  4. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  5. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  6. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  7. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  8. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  9. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

随机推荐

  1. “System.Threading.ThreadAbortException”类型的第一次机会异常在 mscorlib.dll 中发

    问题原因: Thread.Abort 方法 .NET Framework 4  其他版本   1(共 1)对本文的评价是有帮助 - 评价此主题 在调用此方法的线程上引发 ThreadAbortExce ...

  2. 使用OTT处理oracle中的对象(一) OTT配置

    OTT是OCI中访问对象类型数据的重要工具,它将数据库中的对象数据类型或集合类型映射为C++中的结构体类型.OTT是Oracle自带的,但是使用前必须配置一下环境变量.在计算机->属性-> ...

  3. SharePoint 2013 开发——SharePoint Designer 2013工作流

    博客地址:http://blog.csdn.net/FoxDave SharePoint Designer 2013为开发者和高级用户提供了两种创建定制工作流的模式: 基于文本的设计器--即我们一直 ...

  4. SharePoint表单和工作流 - Nintex篇(四)

    博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 "Manage workflow constants" 管理工作流常量.这里可以管理工作流中 ...

  5. 补码复习的好例子---Int范围的科学解释

    Int范围的科学解释 这得从二进制的原码说起: 如果以最高位为符号位,二进制原码最大为0111111111111111=2的15次方减1=32767 最小为1111111111111111=-2的15 ...

  6. 使用HackRF+GNU Radio 破解吉普车钥匙信号

    引文 我最近对软件定义的无线电技术(SDR)产生了浓厚的兴趣,而我对其中一款流行的SDR平台(HackRF)也产生了兴趣,而其频率接收的范围也在1MHz ~6GHz之间(范围较广).而这里也需要提及一 ...

  7. webservice basics

    =====================Quote starts================== JAX-WS (JavaTM API for XML-Based Web Services)规范 ...

  8. 安装webmin

    wget http://nchc.dl.sourceforge.net/project/webadmin/webmin/1.740/webmin-1.740.tar.gz 解压缩文件,命令是:tar ...

  9. 一个比较完整的Inno Setup 安装脚本

    一个比较完整的Inno Setup 安装脚本,增加了对ini文件设置的功能,一个安装包常用的功能都具备了. [Setup] ; 注: AppId的值为单独标识该应用程序. ; 不要为其他安装程序使用相 ...

  10. caffe编译关于imread问题的解决

    change Makefile: LIBRARIES += glog gflags protobuf leveldb snappy \ lmdb boost_system hdf5_hl hdf5 m ...