【CodeForces 602C】H - Approximating a Constant Range(dijk)
Description
In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and yif and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.
A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.
You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.
Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 400, 0 ≤ m ≤ n(n - 1) / 2) — the number of towns and the number of railways respectively.
Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≤ u, v ≤ n, u ≠ v).
You may assume that there is at most one railway connecting any two towns.
Output
Output one integer — the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output - 1.
Sample Input
4 2
1 3
3 4
2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
-1
5 5
4 2
3 5
4 5
5 1
1 2
3
Hint
In the first sample, the train can take the route and the bus can take the route
. Note that they can arrive at town4 at the same time.
In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.
因为任意一对城市之间都有一条直通的路,要么是铁路要么是公路,因此1到n城市一定有铁路或公路,是铁路,就再去找公路的最短路,否则就找铁路的最短路。
#include<stdio.h> const int maxn=0x7fff;
long long n,m,s,e,t[][],u[][],dist[];
void dijk(int v0,long long r[][])
{
bool b[];
for(int i=; i<=n; i++)
{
dist[i]=r[v0][i];
b[i]=false;
}
dist[v0] = ;
b[v0] = true;
for(int i=; i<=n; i++)
{
long long mindis=maxn;
int u = v0;
for(int j=; j<=n; j++)
if((!b[j]) && dist[j]<mindis)
{
u = j;
mindis = dist[j];
}
b[u]=true;
for(int j=; j<=n; j++)
if((!b[j]) && r[u][j]<maxn)
if(dist[u] + r[u][j] < dist[j])
dist[j] = dist[u] + r[u][j];
}
}
int main()
{
scanf("%lld%lld",&n,&m);
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
t[i][j]=maxn;
u[i][j]=;
}
for(int i=; i<m; i++)
{
scanf("%lld%lld",&s,&e);
t[s][e]=t[e][s]=;
u[s][e]=u[e][s]=maxn;
}
if(u[][n]==)//road直达,铁路的最短路
dijk(,t);
else
dijk(,u);
if(dist[n]>=maxn)
printf("-1\n");
else
printf("%lld\n",dist[n]); return ;
}
【CodeForces 602C】H - Approximating a Constant Range(dijk)的更多相关文章
- 【linux命令】setterm控制终端属性命令(中英文)
[linux命令]setterm控制终端属性命令(中英文) 2018年03月23日 17:13:44 阅读数:489 标签: linux 更多 个人分类: linux 摘自:https://blog. ...
- 【完全开源】知乎日报UWP版(下篇):商店APP、github源码、功能说明。Windows APP 良心出品。
目录 说明 功能 截图+视频 关于源码和声明 说明 陆陆续续大概花了一个月的时间,APP算是基本完成了.12月份一直在外出差,在出差期间进行了两次功能完善,然后断断续续修补了一些bug,到目前为止,我 ...
- 【Unity3D实战】方块跑酷初级开发实战(一)
[Unity3D实战]方块跑酷初级开发实战(一) 欢迎大家来到LDS的博客,今天开始我们讲解一下跑酷类游戏的基本操作,本文为原创,视频请观看[ http://www.mkcode.net/html/u ...
- 【OCP|052】OCP最新题库解析(052)--小麦苗解答版
[OCP|052]OCP最新题库解析(052)--小麦苗解答版 OCP最新题库解析历史连接(052):http://mp.weixin.qq.com/s/bUgn4-uciSndji_pUbLZfA ...
- 转载 【.NET基础】--委托、事件、线程(2) https://www.cnblogs.com/chengzish/p/4569912.html
[.NET基础]--委托.事件.线程(2) 本文介绍event的使用以及原理,本文接上一篇文章的Demo继续[下载上一篇Demo] 上一篇我们在类(dg_SayHi.cs)里面定义代理了4个Del ...
- 转载 【.NET基础】--委托、事件、线程(1) https://www.cnblogs.com/chengzish/p/4559268.html
[.NET基础]--委托.事件.线程(1) 1,委托 是存放方法的指针的清单,也就是装方法的容器 A, 新建winform项目[01委托],项目中添加dg_SayHi.cs 委托类 用于存储方法 ...
- 【UOJ#51】【UR #4】元旦三侠的游戏(博弈论)
[UOJ#51][UR #4]元旦三侠的游戏(博弈论) 题面 UOJ 题解 考虑暴力,\(sg[a][b]\)记录\(sg\)函数值,显然可以从\(sg[a+1][b]\)和\(sg[a][b+1]\ ...
- 【FICO系列】SAP FI验证故障排除(调试)
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FI验证故障排除(调试) ...
- 【Geek议题】合理的VueSPA架构讨论(下)
接上篇<[Geek议题]合理的VueSPA架构讨论(上)>传送门. 自动化维护登录状态 登录状态标识符跟token类似,都是需要自动维护有效期,但也有些许不同,获取过程只在用户登录或注册的 ...
随机推荐
- UVA 12730 Skyrk's Bar --期望问题
题意:有n个地方,现在要站人进去,而每两个人之间至少要隔k个空地,问这n个地方能站的人数的期望是多少. 分析:考虑dp[i]表示 i 个地方能站的期望数,从左往右推, 如果i-k-1<1,那么最 ...
- eclipse菜单解释及中英对照《二》
上篇文章主要介绍了eclipse中每个大的标题下的中英文及其用法. 感谢http://blog.csdn.net/li_jinjian2005/article/details/2831641这个博主. ...
- 第22章 DLL注入和API拦截(3)
22.6 API拦截的一个例子 22.6.1 通过覆盖代码来拦截API (1)实现过程 ①在内存中对要拦截的函数(假设是Kernel32.dll中的ExitProcess)进行定位,从而得到它的内存地 ...
- CSS3弹性伸缩布局(二)——flex布局
上一篇博客<CSS3弹性伸缩布局(一)——box布局>介绍了旧版本的box布局,而这篇博客将主要介绍最新版本的flex布局的基础知识. 新版本简介 新版本的Flexbox模型是2012年9 ...
- *.location.href 用法:
*.location.href 用法: top.location.href=”url” 在顶层页面打开url(跳出框架) self.location.href=”url” ...
- 20Mybatis_订单商品数据模型_一对一查询——resultType和resultMap两种方式以及两种方式的总结
上一篇文章分析了数据模型,这篇文章就给出一个需求,这个需求是一对一查询,并完成这个需求. ------------------------------------------------------- ...
- linux64需要增加的依赖库
sudo apt-get install git-core gnupg flex bison gperf build-essential \ zip curl zlib1g-dev gcc-multi ...
- 未能加载文件或程序集“XXXXX”或它的某一个依赖项。试图加载格式不正确的程序。
未能加载文件或程序集“FastColoredTextBox, Version=2.10.5.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项.试图加载 ...
- System类及其常用函数
System 类包含一些有用的类字段和方法.它不能被实例化. 常用方法: 1.static void arraycopy(Object src, int srcPos, Object dest, in ...
- python下RSA 加密/解密,签名/验证
基于win7 + python3.4 原文是py2环境,而我的环境是py3,所以对原代码做了修改:decode(), encode() import rsa # 生成密钥 (pubkey, privk ...