【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类似,都是需要自动维护有效期,但也有些许不同,获取过程只在用户登录或注册的 ...
随机推荐
- jquery/js特效代码总结(一):tab切换
jquery实现tab切换: html代码: <ul class="tabs" id="tabs01"> <li><a href= ...
- Params 方法参数
params,ref,out 方法参数 示例 在下面的方法使用中 OpenWindow(params object[] args) 传递的参数args添加了params修饰 public void O ...
- 解决Linux中java.net.UnknownHostException: oracledb.sys.iflashbuy.com问题
Linux环境报java.net.UnknownHostException: oracledb.sys.iflashbuy.com,原因为Linux下无法解析oracledb.sys.iflashbu ...
- 运维工作中常用到的几个rsync同步命令
作为一个运维工程师,经常可能会面对几十台.几百台甚至上千台服务器,除了批量操作外,环境同步.数据同步也是必不可少的技能.说到“同步”,不得不提的利器就是rsync. 下面结合本人近几年运维工作中对这一 ...
- iOS页面传值方式
普遍传值方式如下: 1.委托delegate方式: 2.通知notification方式: 3.block方式: 4.UserDefault或者文件方式: 5.单例模式方式: 6.通过设置属性,实现页 ...
- DevExpress主从表 按组分页一组不足一页为一页--以此记录
本文的主要是说明Dev的报表的主从表,主从表的每一组显示在一页,当一组超出一页,第二页只显示第一组的. 一.每上报表设置图 简单设计图如上 二.后台代码 报表页代码 public partial cl ...
- js 方法重载
function fun1(arm1) { alert(arm1); } function fun2(arm1, arm2) { alert(arm1 + "_" + arm2); ...
- 未能解析此远程名称:'nuget.org' 的解决方法
今天用Nuget下一个程序包时,发现Nuget挂了: 未能解析此远程名称:'nuget.org' . 浏览器打开 http://nuget.org 失败. 使用cmd命令 输入nslookup n ...
- iBatis.Net(C#)数据库查询
引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html 摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...
- powerdesigner 绘制表关系和导出sql
1.生成图(A图,B图) 2.创建关系(palette工具栏里有个线条,主表子表关系连接即可,拖动是由顺序的,一对多即从A表往B表连接) 3.这里小说一下 一开始是CDM模式,可以在软件最顶层看到 ...