CF#333(Div2) C. The Two Routes(最短路)

来源:互联网 发布:关于51单片机与esp8266 编辑:程序博客网 时间:2024/06/06 01:32

题目点我点我点我


C. The Two Routes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Examples
input
4 21 33 4
output
2
input
4 61 21 31 42 32 43 4
output
-1
input
5 54 23 54 55 11 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.




题目大意:给你n个点,m边的地图,这m边是u到v之间有铁路,然后,没有给出的任意2点之间是马路。问一个坐火车,一个坐巴士,两者不能在同一座城市相遇,从1点出发,最后2人都到达n点的时间。


解题思路:两者总会有一个能从1直接到n点的,故两者不能在同一座城市相遇这一条件可以无视,在另一个不能直接到n点的图上跑最短路。


/* ***********************************************┆  ┏┓   ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃       ┃ ┆┆┃   ━   ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃       ┃ ┆┆┃   ┻   ┃ ┆┆┗━┓ 马 ┏━┛ ┆┆  ┃ 勒 ┃  ┆      ┆  ┃ 戈 ┗━━━┓ ┆┆  ┃ 壁     ┣┓┆┆  ┃ 的草泥马  ┏┛┆┆  ┗┓┓┏━┳┓┏┛ ┆┆   ┃┫┫ ┃┫┫ ┆┆   ┗┻┛ ┗┻┛ ┆************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)#define pb push_back#define mp make_pairconst int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define SelfType intSelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())#define all(a) a.begin(), a.end()typedef pair<int, int> pii;typedef pair<long long, long long> pll;typedef vector<int> vi;typedef vector<long long> vll;inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")int Map1[405][405],Map2[405][405];int n,m;int vis[405];void dijkstra(int Map[405][405]){    int res[405];    for(int i=1;i<=n;i++)    {        res[i] = Map[1][i];        vis[i] = 0;    }    for(int i=1;i<=n;i++)    {        int mi = inf_int,v;        for(int j=1;j<=n;j++)        {            if(!vis[j] && res[j]<mi)            {                mi = res[j];                v = j;            }        }        vis[v] = 1;        for(int j=1;j<=n;j++)        {            if(!vis[j] && res[j]>res[v]+Map[v][j])            {                res[j] = res[v] + Map[v][j];            }        }    }    if(res[n]==inf_int)printf("-1\n");    else printf("%d\n",res[n]);}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);ios::sync_with_stdio(0);cin.tie(0);n = read(), m = read();for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            if(i==j)Map1[i][j] = Map2[i][j] = 0;            else Map1[i][j] = Map2[i][j] = inf_int;        }    }    int flag = 0;    for(int i=1;i<=m;i++)    {        int a,b;        a = read(), b = read();        if(abs(a-b)==n-1)flag = 1;        Map2[a][b] = Map2[b][a] = 1;    }for (int i = 1 ; i <= n ; i ++ )    {        for (int j = 1 ; j <= n ; j++ )        {            if(Map2[i][j] == inf_int)            {                Map1[i][j] = Map1[j][i] = 1;            }        }    }if(n*(n-1)/2==m)printf("-1\n");else if(flag)dijkstra(Map1);else dijkstra(Map2);return 0;}





0 0
原创粉丝点击