codeforces 602C- The Two Routes 最短路

The Two Routes CodeForces - 602C

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.

Examples

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

A key point is to notice that he is a complete graph (because the sample space is directly connected with points, and then some connections are given, and the rest are filled in mutually exclusive, so it must be a complete graph, when you When you think of mutually exclusive filling, you should think of the complete picture.)
Then 1-----N must have a way directly, (fill in with if mutually exclusive) and just run dj or floyd.

There is also... The macro definitions of sen are really good to cry, and they are as good as the people of sen. Thank you sen....QAQ....copy it and keep it for use

#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <queue>
#include <cstdio>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <cmath>
#include <vector>
#include <ctime>
#include <bitset>
#include <assert.h>
using namespace std;
#define pb push_back
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define ansn() printf("%d\n",ans)
#define lansn() printf("%lld\n",ans)
#define r0(i,n) for(int i=0;i<(n);++i)
#define r1(i,e) for(int i=1;i<=e;++i)
#define rn(i,e) for(int i=e;i>=1;--i)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define lowbit(a) (a&(-a))
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pll pair<long long,long long>
#define mp(aa,bb) make_pair(aa,bb)
#define lrt rt<<1
#define rrt rt<<1|1
#define X first
#define Y second
#define PI (acos(-1.0))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
const double eps=1e-9;
const int inf=0x3f3f3f3f;
const int maxn=555;
int mp[maxn][maxn];
int main()
{
    int n,m;
    sdd(n,m);
    r0(i,m)
    {
        int u,v;
        sdd(u,v);
        mp[u][v]=mp[v][u]=1;

    }
    if(mp[1][n])
    {
        r1(i,n)
        {
            r1(j,n)
            {
                if(mp[i][j]) mp[i][j]=inf;
                else mp[i][j]=1;
            }
        }
    }
    r1(i,n)
    {
        r1(j,n)
        {
            if(!mp[i][j]) mp[i][j]=inf;
        }
    }
    ll ans;
    r1(k,n)
    r1(i,n)
    r1(j,n)
    if(mp[i][k]+mp[k][j]<mp[i][j])
        mp[i][j]=mp[i][k]+mp[k][j];
     ans=mp[1][n];
    if(mp[1][n]<inf)
        ansn();
    else cout<<-1<<endl;
    return 0;

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325232236&siteId=291194637