您的位置:首页 > 其它

CSU-ACM2017暑假集训比赛8 - B - The Two Routes - CodeForces - 601A

2017-08-27 10:08 411 查看

B - The Two Routes

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.


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.


Example

Input

4 2
1 3
3 4

Output

2

Input

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

Output

-1

Input

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

Output

3


Note

In the first sample, the train can take the route 1→3→4 and the bus can take the route 1→2→4. 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.


题目要求我们求出火车和巴士各自从1城到N城的最短路,两者拥有相同的速度,同时出发且在同一时刻不能处在同一座城市,解出较晚到达一方所花费的时间。

做题时觉得“两车不能同时处于同一城市”这个条件十分重要,一直在想办法避免这种情况出现。但最后讨论才发现实际是个纸老虎,根本不必考虑这种情况的出现:由于所有城市两两相连,又给出了铁路相连的城市序号,那么那些没有声明连接关系的城市就由公路相连。进一步可以推出,必有一种道路将起点和终点直接联系起来,要么是铁路直连了起点和终点,要么是公路直连了起点和终点。

这样一来,不妨先计算火车的最短路。如果它能够直达终点,我们就可以计算巴士的最短路长度,如果存在这样的最短路(即巴士可以到达终点),这个最短路的长度就是所求答案;若巴士不能到达终点,则对应无解。

接着讨论火车不能直达终点的情况。之前已经证明,必有一种车辆是可以直达终点的。那么火车不能直达时说明巴士必可直达。这时,算出火车对应的最短路就得所求答案。

如果火车根本无法到达终点,立马知道无解。

综上,题目所述的情况无非两种,要么无解,不必考虑同时到达同一城市;要么存在令某种车辆直达终点的最短路,以及另一种车辆对应的“次短路”,也不必考虑同时到达同一城市。既然如此,题目的做法也不言自明了,任意最短路算法都可以胜任。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 510, INF = 0x3f3f3f3f;

int N, M;
bool w[maxn][maxn];
struct Node{
int id, step; // 路径长度用结构体成员step保存。
bool vis;
}node[maxn], nodeBus[maxn];

void init(){
memset(w, false, sizeof(w));
for(int i = 0; i < maxn; i++){
node[i].id = nodeBus[i].id = i;
node[i].step = nodeBus[i].step = 0;
node[i].vis = nodeBus[i].vis = false;
}
}

// 做题时没考虑周全,为了防止同时处在同一城市而编了两个bfs,
// 实际上只需一个,执行两次即可。具体实现也较为繁琐,可以取消
// 对是否在某城市并存的判断。不过找最短路思路是正确的,可以参考。
bool bfsTrain(int s){
queue<Node> q;
q.push(node[s]);
node[s].vis = true;
while(!q.empty()){
Node u = q.front();
q.pop();
for(int i = 1; i <= N; i++){
int to = i, now = u.id;
if(w[now][to] && !node[to].vis){
if(to == N){
node[to].step = u.step+1;
return true;
}
node[to].vis = true;
node[to].step = u.step+1;
q.push(node[to]);
}
}
}
return false;
}
bool bfsBus(int s){
queue<Node> q;
q.push(nodeBus[s]);
nodeBus[s].vis = true;
while(!q.empty()){
Node u = q.front();
q.pop();
for(int i = 1; i <= N; i++){
int to = i, now = u.id;
if(!w[now][to] && !nodeBus[to].vis && node[to].step != u.step+1){
if(to == N){

4000
nodeBus[to].step = u.step+1;
return true;
}
nodeBus[to].vis = true;
nodeBus[to].step = u.step+1;
q.push(nodeBus[to]);
}
}
}
return false;
}

int main(){
#ifdef TEST
freopen("test.txt", "r", stdin);
#endif // TEST

while(cin >> N >> M){
init();
for(int i = 1; i <= M; i++){
int a, b;
scanf("%d%d", &a, &b);
w[a][b] = w[b][a] = true;
}
bfsTrain(1);
bfsBus(1);
if(!node
.step || !nodeBus
.step)
printf("-1\n");
else
printf("%d\n", max(node
.step, nodeBus
.step));
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 最短路