【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类似,都是需要自动维护有效期,但也有些许不同,获取过程只在用户登录或注册的 ...
随机推荐
- CSS 属性 - position讲解
postion 属性定义了一个元素在页面布局中的位置以及对周围元素的影响.该属性共有5个值: 1. position: static2. position: inherit3. position: r ...
- (已解决) 未能加载文件或程序集“Newtonsoft.Json, Version=4.0.0.0, Culture=neutral,
在项目web.config里面添加: <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4F ...
- UICollectionView使用
本文原文 原文转自 1.1. Collection View 全家福: UICollectionView, UITableView, NSCollectionView n 不直接等效于NSColl ...
- 024医疗项目-模块二:药品目录的导入导出-HSSF导入类的学习
我们之前学习了怎么把数据的数据导出来保存到Excle中,这篇文章我们学习怎么Excel数据导出然后插入到数据库中. 我们先学习HSSF怎么用来导出数据. 看官方教程步骤如下: 第一步: 创建一个wor ...
- RDLC直接打印帮助类
代码 /// <summary> /// 打印帮助类 /// </summary> public class PrintHelper { private int m_curre ...
- 10301 MySQL各个版本区别及下载
参考:http://www.admin10000.com/document/62.html MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会 ...
- Java中的IO流系统详解(转载)
摘要: Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java ...
- 【转】【WPF】WPF样式(Style)—触发器
样式(Styles)由三部分构成:设置器(Setter).触发器(Triggers).资源(Resources). (1)触发器,让样式的使用更加准确.灵活和高效. (2)触发器(Triggers)主 ...
- Elasticsearch 相关名词理解
Cluster包含多个node,Indices不应该理解成动词索引,Indices可理解成关系数据库中的databases,Indices可包含多个Index,Index对应关系数据库中的databa ...
- MTK android 工程中如何修改照片详细信息中机型名
每一个项目的机型名都不相同,因此拍出来的照片需要更改详细信息中的机型名. 那么,具体在哪里修改照片详细信息机型名呢 路径信息:/ALPS.JB3.TDD.MP.V2_TD_xxx/mediatek/c ...