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

题目点我点我点我


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




题目大意:给你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_pair
const 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 int
SelfType 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`routes.IgnoreRoute`、`routes.MapPageRoute`和`routes.MapRoute`是在ASP.NET中用于定义和配置路由规则的方法。它们之间的区别如下: 1. `routes.IgnoreRoute`:这个方法用于忽略某些URL,让它们不被路由系统处理。通常用于忽略某些特定的静态文件或资源,如图片、CSS文件、JavaScript文件等。示例用法如下: ```csharp routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); ``` 上述示例中,我们忽略了以`.axd`为扩展名的资源文件。 2. `routes.MapPageRoute`:这个方法用于定义并注册ASP.NET Web Forms页面的路由规则。它将指定的URL模式映射到实际的物理文件,并可传递参数给目标页面。示例用法如下: ```csharp routes.MapPageRoute("ProductDetails", "products/{category}/{id}", "~/ProductDetails.aspx"); ``` 上述示例中,我们定义了一个名为"ProductDetails"的路由规则,将匹配形如`/products/electronics/123`的URL,并将请求路由到物理文件`ProductDetails.aspx`。其中,`{category}`和`{id}`是路由参数。 3. `routes.MapRoute`:这个方法用于定义并注册ASP.NET MVC控制器和动作方法的路由规则。它将指定的URL模式映射到相应的控制器和动作方法。示例用法如下: ```csharp routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); ``` 上述示例中,我们定义了一个名为"Default"的路由规则,将匹配形如`/Home/Index`的URL,并将请求路由到名为`HomeController`的控制器的`Index`动作方法。其中,`{controller}`、`{action}`和`{id}`是路由参数,`HomeController`和`Index`是默认值。 总结来说,`routes.IgnoreRoute`用于忽略某些URL,`routes.MapPageRoute`用于定义Web Forms页面的路由规则,而`routes.MapRoute`用于定义MVC控制器和动作方法的路由规则。它们各自适用于不同的场景和技术。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值