URAL 1930 Ivan's Car(BFS)
Ivan's Car
Memory limit: 64 MB
Input
Output
Samples
input | output |
---|---|
3 2 |
1 |
3 3 |
0 |
#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)的更多相关文章
- 1930. Ivan's Car(spfa)
1930 简单二维 标记一下是上坡还是下坡 #include <iostream> #include<cstdio> #include<cstring> #incl ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
随机推荐
- “System.Threading.ThreadAbortException”类型的第一次机会异常在 mscorlib.dll 中发
问题原因: Thread.Abort 方法 .NET Framework 4 其他版本 1(共 1)对本文的评价是有帮助 - 评价此主题 在调用此方法的线程上引发 ThreadAbortExce ...
- 使用OTT处理oracle中的对象(一) OTT配置
OTT是OCI中访问对象类型数据的重要工具,它将数据库中的对象数据类型或集合类型映射为C++中的结构体类型.OTT是Oracle自带的,但是使用前必须配置一下环境变量.在计算机->属性-> ...
- SharePoint 2013 开发——SharePoint Designer 2013工作流
博客地址:http://blog.csdn.net/FoxDave SharePoint Designer 2013为开发者和高级用户提供了两种创建定制工作流的模式: 基于文本的设计器--即我们一直 ...
- SharePoint表单和工作流 - Nintex篇(四)
博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 "Manage workflow constants" 管理工作流常量.这里可以管理工作流中 ...
- 补码复习的好例子---Int范围的科学解释
Int范围的科学解释 这得从二进制的原码说起: 如果以最高位为符号位,二进制原码最大为0111111111111111=2的15次方减1=32767 最小为1111111111111111=-2的15 ...
- 使用HackRF+GNU Radio 破解吉普车钥匙信号
引文 我最近对软件定义的无线电技术(SDR)产生了浓厚的兴趣,而我对其中一款流行的SDR平台(HackRF)也产生了兴趣,而其频率接收的范围也在1MHz ~6GHz之间(范围较广).而这里也需要提及一 ...
- webservice basics
=====================Quote starts================== JAX-WS (JavaTM API for XML-Based Web Services)规范 ...
- 安装webmin
wget http://nchc.dl.sourceforge.net/project/webadmin/webmin/1.740/webmin-1.740.tar.gz 解压缩文件,命令是:tar ...
- 一个比较完整的Inno Setup 安装脚本
一个比较完整的Inno Setup 安装脚本,增加了对ini文件设置的功能,一个安装包常用的功能都具备了. [Setup] ; 注: AppId的值为单独标识该应用程序. ; 不要为其他安装程序使用相 ...
- caffe编译关于imread问题的解决
change Makefile: LIBRARIES += glog gflags protobuf leveldb snappy \ lmdb boost_system hdf5_hl hdf5 m ...