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. HDU 5382 莫比乌斯反演

    题目大意: 求S(n)的值 n<=1000000 这是官方题解给出的推导过程,orz,按这上面说的来写,就不难了 这里需要思考的就是G(n)这个如何利用积性函数的性质线性筛出来 作为一个质数,那 ...

  2. xcode 5.0 以上去掉icon高亮方法&iOS5白图标问题

    之前的建议方法是把在xxx.info.plist文件中把 icon already includes gloss and bevel effects 设置YES 在Xcode5下,反复实现不成功,今天 ...

  3. ZOJ 3329 - One Person Game

    题意:每次筛三个骰子面分别为k1,k2,k3,如果三个骰子的值分别为a,b,c则得分置0,否则得到分数加上三个骰子的值的和,如果得分大于等于n则结束游戏. 设E[i]表示当前得到i分时结束游戏的期望. ...

  4. Eclipse中一些快捷键

    用到哪里更新到哪里~~~ 1,代码自动对齐 ctrl+shift+f 2,自动填充相关的包 alt+/   注意选择好需要的包 3,注释某几行代码 选定后ctrl+/ 4,设置自动补全 最简单的修改方 ...

  5. BZOJ 4027 兔子与樱花

    原来想的是给所有点排序....但是要修改啊...然后发现对于儿子排序就可以了. #include<iostream> #include<cstdio> #include< ...

  6. 自动打补丁Bat指令

    自动打补丁 补丁放于hotfix文件夹下 for %%i in (hotfix\*.exe) do start /wait %%i /z /u

  7. 13、SQL基础整理(流程控制begin……end)

    流程控制 begin ……end将一个语句块包含起来,中间可以写任何语句 格式: begin--开始 select *from student end--结束 if declare @bianlian ...

  8. JS手机定位地理位置

    /** * 以下为html5代码,获取地理位置 */ /** * 设置地址 */ function setAddress(json) { var position = document.getElem ...

  9. mysql锁表机制及相关优化

    (该文章为方便自己查阅,也希望对大家有所帮助,转载于互联网) 1. 锁机制 当前MySQL支持 ISAM, MyISAM, MEMORY (HEAP) 类型表的表级锁,BDB 表支持页级锁,InnoD ...

  10. MINIX3 进程通信分析

    MINIX3 进程通信分析 6.1MINIX3 进程通信概要 MINIX3 的进程通信是 MINIX3 内核部分最重要的一个部件,我个人认为其实这 是内核中的“内核”,怎么来理解这个概念呢?其实 MI ...