【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类似,都是需要自动维护有效期,但也有些许不同,获取过程只在用户登录或注册的 ...
随机推荐
- 页面分享代码share
在开发一个页面的时候常常会有这么一个小功能,就是分享该页面中的信息. 常见的分享代码有百度分享,JiaThis分享插件,bshare分享插件等,我主要分享一下自定义分享代码,如下: function ...
- python-可变迭代对象在for循环中的风险Risk in FOR loop while looping mutable iterable object
>>> a = [1,2,3,4,5,6] >>> for item in a: ... a.remove(item) ... >>> a [2, ...
- POJ 2653 Pick-up sticks --队列,几何
题意: 按顺序扔木棒,求出最上层的木棒是哪些. 解法: 由于最上层的木棒不超过1000个,所以用一个队列存储最上层的木棒,每次扔出一个木棒后,都与队列中的木棒一一判断,看此木棒是否在某一最上层的木棒的 ...
- View (二) 自定义属性 自定义属性的格式详解
自定义属性格式一共有十种: 1. reference:参考某一资源ID. 2. color:颜色值. 3. boolean:布尔值. 4. dimension:尺寸值. 5. float:浮点值. 6 ...
- ArcGis 统计方法
from:http://blog.sina.com.cn/s/blog_4177d50b0100fjbg.html 概述 一般常用的统计功能例如:唯一字段统计.数据行数统计.数据值求和统计等. 1.基 ...
- mysql及php命名规范
一.mysql命名规范 1.设计原则 1) 标准化和规范化数据的标准化有助于消除数据库中的数据冗余.标准化有好几种形式,但 Third Normal Form(3NF)通常被认为在性能.扩展性和数据完 ...
- 【WPF】Winform调用WPF窗体注意事项
1.需要添加一些引用 2.调用处使用如下方法进行调用 Window win= new Window(); ElementHost.EnableModelessKeyboardInterop(win) ...
- tftp从linux下载文件
1,背景: 当我们ssh到一台linux上时候,从linux上下载一些文件,方案如下: 1.1通过sftp:通过win7 ftp客户端连接到linux去下载文件. 1.2通过tftp 2,问题 有些l ...
- 模拟alert和confirm
啥也不说,先上图,有图有真相 :) 现在绝大多数网站都不用自带的alert和confirm了,因为界面太生硬了.因此这个插件就这样产生了... 来看插件的实现代码吧: (function () { $ ...
- UltraEdit编辑器使用心得之正则表达式篇
ultraEdit 中通过Ctrl+R 可以快速进行文本替换等处理操作,如果在这中间用一些正则表达式那将帮助NI更高效的进行文字处理操作,相关正则表达式列述如下: % 匹配行首 - 表示搜索字符串必须 ...