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 ≤ nu ≠ 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

Input

Output

Input

Output
-
Input Output

题意:

在北极,有n个城镇(编号从1到n)和m个双向铁路。这个城镇有一个简单的道路网络 - 对于每一对不同的城镇x和y来说,当且仅当它们之间没有铁路时候,在x,y两镇之间必定有一条双向的公路。

从任意一个小镇走一条铁路或一条公路到一个不同的小镇需要一个小时。火车和巴士同时离开城镇1。他们两个都有相同的目的地n,他们并没有在路上停下来(但他们可以在城镇n等待)。火车只能沿着铁路行驶,公共汽车只能沿着公路行驶。

你需要规划出火车和巴士的路线;

每条路线可以多次使用任何公路或者铁路。要考虑的最重要的方面之一是安全 - 为了避免在铁路口岸发生事故,火车和公共汽车不能同时到达同一个镇(n镇除外)。在这些限制下,两辆车到达城镇n所需的最少小时数中较大的那个(巴士和火车到达时间的最大值)是多少?请注意,巴士和火车不需要在同一时间到达城镇n,但是可以同时到达。

思路:

这个题真的一句话都不能丢。其中很重要的一句话是      当且仅当它们之间没有铁路时候,在x,y两镇之间必定有一条双向的公路。也就是说起点和终点一定会有一条路,要么是铁路,要么是公路。

如果是有铁路直连,那么直接求公路的起点到终点的最短路。如果是有公路直连,那么直接求铁路的起点到终点的最短路。就是一个简单的最短路的题目!

代码:

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie();
using namespace std;
#define inf 0x3f3f3f3f
int map1[][];
int map2[][];
int dis[],visit[];
int n,m;
int dijstra(int map1[][])
{
int i,j,pos=,min,sum=;
memset(visit,,sizeof(visit));
for(i=; i<=n; ++i)
{
dis[i]=map1[][i];
}
visit[]=;
dis[]=;
for(i=; i<n; i++)
{
min=inf;
for(j=; j<=n; ++j)
{
if(visit[j]==&&min>dis[j])
{
min=dis[j];
pos=j;
}
}
visit[pos]=;
for(j=; j<=n; ++j)
{
if(visit[j]==&&dis[j]>dis[pos]+map1[pos][j])
dis[j]=dis[pos]+map1[pos][j];
}
}
return dis[n];
}
int main()
{
//IO;
int i,j;
while(~scanf("%d%d",&n,&m))
{
for(i=; i<=n; ++i)
{
for(j=; j<=n; ++j)
{
map1[i][j]=inf;
map2[i][j]=inf;
}
}
int a,b,c;
for(i=; i<=m; ++i)
{
scanf("%d%d",&a,&b);
c=;
map1[a][b]=map1[b][a]=c;
}
for(i=; i<=n; ++i)
{
for(j=; j<=n; ++j)
{
if(map1[i][j]==inf)
map2[i][j]=;
}
}
int count=;
if(map1[][n]==||map1[n][]==)
count=dijstra(map2);
else
count=dijstra(map1);
if(count==inf)
printf("-1\n");
else
printf("%d\n",count);
}
return ;
}

CodeForces 602C The Two Routes(最短路)的更多相关文章

  1. [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路

    14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...

  2. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  3. 【50.00%】【codeforces 602C】The Two Routes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. codeforces 601A The Two Routes(最短路 flody)

    A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. ACM学习历程—CodeForces 601A The Two Routes(最短路)

    题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...

  6. 【CodeForces 602C】H - Approximating a Constant Range(dijk)

    Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...

  7. Codeforces 545E. Paths and Trees 最短路

    E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...

  8. Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  9. Shortest Path Codeforces - 59E || 洛谷P1811 最短路_NOI导刊2011提高(01)

    https://codeforces.com/contest/59/problem/E 原来以为不会..看了题解发现貌似自己其实是会的? 就是拆点最短路..拆成n^2个点,每个点用(i,j)表示,表示 ...

随机推荐

  1. json属性名为什么要双引号?

    原因一: 更加规范,利于解析 原因二: 避免class等关键字引起的不兼容问题 原因三: 可能也是最隐晦的: var a = 00; var b = {00: 12}; a in b; --> ...

  2. 引用类型 ( 对象定义 )——Object 类型

    本文地址:http://www.cnblogs.com/veinyin/p/7607100.html  创建实例 new 操作符后跟构造函数 var people = new Object(); pe ...

  3. 简易安装sqoop

    版本 :hive-0.13.1-cdh5.3.6.tar.gz 1:解压 然后 进到 conf 目录 修改 sqoop-env.sh   2:如果使用mysql 数据库 要将 mysql驱动包拷贝到 ...

  4. 用体渲染的方法在Unity中渲染云(18/4/4更新)

    github: https://github.com/yangrc1234/VolumeCloud 更新的内容在底部 最近在知乎上看到一篇文章讲云层的渲染(https://zhuanlan.zhihu ...

  5. hdu 5319 Painter(杭电多校赛第三场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319 Painter Time Limit: 2000/1000 MS (Java/Others)   ...

  6. 多维尺度变换MDS(Multidimensional Scaling)

    流形学习(Manifold Learning)是机器学习中一大类算法的统称,流形学习是非线性的降维方法(an approach to non-linear dimensionality reducti ...

  7. (三)Spring 之AOP 详解

    第一节:AOP 简介 AOP 简介:百度百科: 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个 ...

  8. OpenSSL 给自己颁发根证书,由根证书签发下级证书的步骤。

    1.建立根证书 (1)生成私钥 openssl genrsa -des3 -out CAroot.key 2048.产生一个2048位的私钥,在安装的openssl目录下调用openssl命令. 需要 ...

  9. 纯js的N级联动列表框 —— 基于jQuery

    多个列表框联动,不算是啥大问题,但是却挺麻烦,那么怎么才能够尽量方便一点呢?网上搜了一下,没发现太好用的,于是就自己写了一个.基于jQuery,无限级联动,支持下拉列表框和列表框. 先说一下步骤和使用 ...

  10. 翻译:MLAPP(2.1节 概率概述)

    笔者:尝试翻译MLAPP(Machine Learning: a Probabilistic Perspective)一书,供机器学习的学者参考,如有错误理解之处请指出,不胜感激!(如需转载,请联系本 ...