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.
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 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.
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
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.
题意:有n个城镇,两个城镇之间要么有一条铁路,要么有一条公路,通过一条铁路和一条公路的时间均为1。有一辆汽车和一辆火车,他们同时从点1出发,要到达点n,中间不会停留。为了避免交通事故,他们不能在同一时刻到达同一个点(除开终点n)。问两辆车都到达n点的最短时间。
题解:如果1到n是铁路相连,那么火车一定走这条路,汽车一定是走最短路。反之亦然。所以判断一下是哪种方式是可以直达的,再求一下另外一种方式的最短路就可以了。
代码:
#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#include<queue>
#include<set>
#include<stack>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mst(a) memset(a, 0, sizeof(a))
#define M_P(x,y) make_pair(x,y)
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
const int lowbit(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 1e9+7;
const ll inf =(1LL<<62) ;
const int MOD = 1e9 + 7;
const ll mod = (1LL<<32);
const int N = 3e5+7;
const int M=100010;
const int maxn=2e3+7;
template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;}
template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}
int read(){
int v = 0, f = 1;char c =getchar();
while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}
while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();
return v*f;}
int map1[410][410],map2[410][410]; //记录火车的路和大巴的路
int n,m;
void dij(int map[][410])
{
int res[410]; //记录结果
int vis[410]; //访问标记
int v,min; //每相连点的最小下标和最短的路
for(int i = 1 ; i <= n ; i++ ) //初始化
{
vis[i] = 0;
res[i] = map[1][i];
}
for (int i = 1 ; i <= n ; i++ )
{
min = 1<< 29;
for (int j = 1 ; j <= n ; j++ )
{
if (vis[j] == 0 && res[j] < min ) //遍历,找出与这个点相连的最短路
{
v = j;
min = res[j];
}
}
vis[v] = 1;
for (int j = 1 ; j <= n ; j++ )
{
if(vis[j] == 0 && res[j] > map[v][j] + res[v]) //check
{
res[j] = map[v][j] +res[v];
}
}
}
if (res[n] == INF) //如果最后的值没有改变 说明不可能
{
puts("-1");
return ;
}
else
cout<<res[n];
}
int main()
{
n=read(),m=read();
for (int i = 1 ; i <= n ; i++ ) //初始化
{
for (int j = 1 ; j <= n ; j++ )
{
if (i ==j)
{
map1[i][j] = 0;
map1[i][j] = 0;
}
else
{
map1[i][j] = map1[j][i] = INF;
map2[i][j] = map2[j][i] = INF;
}
}
}
int flag = 0;
for (int i = 0 ; i < m ; i++ )
{
int a,b;
scanf("%d%d",&a,&b);
if ( (a == 1 && b == n) || (a == n && b == 1) ) //一定有一条路可以直达,所以判断是火车还是巴士
{
flag = 1;
}
map1[a][b] = 1;
map1[b][a] = 1;
}
for (int i = 1 ; i <= n ; i ++ )
{
for (int j = 1 ; j <= n ; j++ )
{
if ( map1[i][j] == INF || map1[i][j] == 0)
{
map2[i][j] = 1;
map2[j][i] = 1;
}
}
}
if (m == n*(n-1)/2) //如果只有火车有路径
{
puts("-1");
return 0;
}
if (flag) //如果火车可以直达
{
dij(map2);
}
else if (!flag ) //如果大巴可以直达
{
dij(map1);
}
return 0;
}