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. JSON.parse()和eval()区别

    JSON.parse()只会将标准的Json字符串(key和value都由双引号引起来,最外面用单引号括住)转为JSON对象. eval()在转换字符串的时候是比较松的,即使不是标准的Json字符串也 ...

  2. Cisco IOS Software Activation Command Reference

    clear license agent : to clear license agent statistics counters or connection statistics (in privil ...

  3. SharePoint安全 - SharePoint网站常用页面URL索引

    博客地址 http://blog.csdn.net/foxdave 一. 主要网站内容 首页 /default.aspx /Pages/default.aspx 网站设置 /_layouts/sett ...

  4. julia解无忧公主的数学时间097.jl

    julia解无忧公主的数学时间097.jl #=""" julia解无忧公主的数学时间097.jl http://mp.weixin.qq.com/s?__biz=MzI ...

  5. 使用变量替换批量部署GoldenGate

    GoldenGate运行时允许在参数文件中动态指定一个值,即在参数文件中使用一个变量,而不是一个静态的值,当启动OGG进程时,根据环境动态加载此变量的值,达到在不同环境中,通过变量定义,实现多个环境的 ...

  6. HTML--5 JavaScript

    一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司 ...

  7. JSON解析和XML解析

    一. XML:用到一个开源解析类,GDataXMLNode(将其加入项目中),添加libxml2.dylib框架 经常用到的方法: 1.- (id)initWithXMLString:(NSStrin ...

  8. 程序员最爱 Mac、JS 是最热门技术

    概况: 今年,有超过5万名开发者向我们分享了他们是谁,做什么工作,以及他们的成果.通过本文,你将看到有史以来最为全面的一次开发者情况调查的结果. 每8秒钟,就会有一位开发者在Stack Overflo ...

  9. PHP中目录的操作

    文件的操作:创建文件,删除文件,重命名文件rename(),移动/复制文件,读取,大小(PHP都有内置的函数) 目录的操作:创建目录(有),删除目录,复制目录,统计目录大小,遍历(自己定义函数) 一. ...

  10. Drools规则文件结构说明

    一.规则文件构成 package(规则文件所在包) import(导入java包) global(规则文件中的"全局变量") function(函数) query(查找) rule ...