CF601A.The Two Routes

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 y if 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.

在荒诞国(Absurdistan),共有 nn 个城镇(编号为 11 至 nn)和 mm 条双向铁路。此外,还存在一个极其简单的公路网络:对任意两个不同的城镇 xx 和 yy,当且仅当 xx 与 yy 之间没有铁路连接时,才存在一条连接 xx 和 yy 的双向公路。无论乘坐铁路还是公路前往另一个城镇,耗时均为恰好一小时。

一列火车和一辆公交车同时从城镇 11 出发,目的地均为城镇 nn,途中均不作停留(但可在城镇 nn 等待)。火车只能沿铁路行驶,而公交车只能沿公路行驶。

你需要为这两种交通工具规划行驶路线;每条路线均可多次重复使用任意公路或铁路。其中一项最重要的考量因素是安全性——为避免在铁路道口发生事故,火车与公交车不得同时抵达同一座城镇(城镇 nn 除外)。

在上述约束条件下,两者均抵达城镇 nn 所需的最短时间(即公交车与火车到达时间中的较大值)是多少?注意:公交车与火车不必在同一时刻抵达城镇 nn,但允许如此。

输入格式

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.

输入的第一行包含两个整数 nn 和 mm(2≤n≤4002 \leq n \leq 400,0≤m≤n(n−1)/20 \leq m \leq n(n-1)/2),分别表示城镇的数量和铁路的数量。

接下来的 mm 行中,每行包含两个整数 uu 和 vv,表示城镇 uu 与城镇 vv 之间有一条铁路(1≤u,v≤n1 \leq u, v \leq n,u≠vu \neq v)。

你可以假设任意两个城镇之间至多只有一条铁路相连。

输出格式

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.

输出一个整数——即较晚到达城镇 nn 的车辆的最早可能到达时间。若至少有一辆车辆无法到达城镇 nn,则输出 −1-1。

输入输出样例

  • 输入#1

    4 2
    1 3
    3 4

    输出#1

    2
  • 输入#2

    4 6
    1 2
    1 3
    1 4
    2 3
    2 4
    3 4

    输出#2

    -1
  • 输入#3

    5 5
    4 2
    3 5
    4 5
    5 1
    1 2

    输出#3

    3

说明/提示

In the first sample, the train can take the route and the bus can take the route . Note that they can arrive at town 4 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.

在第一个样例中,火车可以走路线 ,而公交车可以走路线 。注意,它们可以同时抵达第 4 号城镇。

在第二个样例中,Absurdistan 由铁路工人统治。该国没有公路,因此公交车无法到达第 4 号城镇。

输入解题思路,AI测评打分。不知道怎么写?

首页