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. LintCode 394: First Will Win

    LintCode 394: First Will Win 题目描述 有n个硬币排成一条线.两个参赛者轮流从右边依次拿走1或2个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是 ...

  2. 【译】DTD - Entities

    原文:DTD - Entities 实体用于定义XML文档中特殊字符的快捷方式. 实体主要有四种类型: 内置实体(Built-in entities) 字符实体(Character entities) ...

  3. HTML如何编写为桌面程序

    学过/用过HTML的人应该都知道HTML是标记语言,是在网页上执行/使用的,在这里小编告诉你HTML也可以用来做桌面程序,这种桌面程序一般是微客户端 工具/原料   html dreamweaver ...

  4. VideoJS 与 Framework7 中 fastclick 冲突问题

    Framework7 由于自动启用  fastclick,会导致在 移动端下使用 video.js,控制条上的 播放和音量按钮 点击的时候会触发两次. 解决办法: 1. 全局禁用 fastclick, ...

  5. UML相关说明

    在java开发中,有很多时候我们是在不断的处理类与类之间关系,其中这六种关系是:依赖.关联.聚合.组合.继承.实现. 它们的强弱关系是没有异议的:依赖 < 关联 < 聚合 < 组合& ...

  6. 安装Docker-ce

    Docker Engine改为Docker CE(社区版) 它包含了CLI客户端.后台进程/服务以及API.用户像以前以同样的方式获取.Docker Data Center改为Docker EE(企业 ...

  7. FileZilla 配置备份与还原【转】

    FileZilla是一款免费开源的FTP软件,安装和配置都很简单.在安装目录下的FileZilla Server Interface.xml和FileZilla Server.xml两个文件是程序的配 ...

  8. 【UOJ#38】【清华集训2014】奇数国

    考虑欧拉函数的性质,60很小,压位存下线段树每个节点出现质数. #include<bits/stdc++.h> ; ; typedef long long ll; using namesp ...

  9. Ubuntu 10.04 分辨率调整

    最近学长们看了我的本本都在问我,显卡驱动是不是出现什么问题了···分辨率这么差.当时我的分辨率是1024X768,于是我就想修改我的屏幕分辨率改成1280X800.本来很简单的事情,我做起来却非常的曲 ...

  10. Redis安装和客户端cli常见操作

    安装Redis $ wget http://download.redis.io/releases/redis-4.0.6.tar.gz $ tar xzf redis-4.0.6.tar.gz $ c ...