A. 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 ≤ 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 ≤ 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 town4 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个点,n*(n-1)/2条边,任意两个点之间都有边,现在把边黑白染色,有m条白的边,其他都黑色的。两个人一开始位于1号点,第一个人只走白边,第二个人只走黑边,问两个人都到达n的最短时间。有一个人不能到达就输出-1。可以不同步,就是不必同时到达n

一开始我这想半天。。还想过爆搜。。主要是题面太有误导性

后来突然醒悟,一定有一条1到n的,不管是黑色还是白色

然后只要看另一个人什么时候到就好啦

这题n才400这么小,都能folyd了

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void write(LL a)
{
if (a<){printf("-");a=-a;}
if (a>=)write(a/);
putchar(a%+'');
}
inline void writeln(LL a){write(a);printf("\n");}
int n,m;
int dis[][];
bool mrk[][],fuckyou;
int main()
{
n=read();m=read();
memset(dis,/,sizeof(dis));
for (int i=;i<=m;i++)
{
int x=read(),y=read();
mrk[x][y]=mrk[y][x]=;
}
if (mrk[][n])fuckyou=;
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (fuckyou^mrk[i][j])dis[i][j]=1ll;
for (int k=;k<=n;k++)
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
if (dis[][n]>=)printf("-1");
else printf("%d\n",dis[][n]);
}

cf601A

cf601A The Two Routes的更多相关文章

  1. CF601A 【The Two Routes】

    看数据范围,然后果断邻接矩阵$Floyd$啊 对于公路和铁路,各建一个图,分别跑最短路,然后取最大值即可 #include<iostream> #include<cstdio> ...

  2. routes.rb和link_to的一些规则

    rails文档中描述了一个知识,link_to方法用于产生链接,但链接是根据routes.rb中的路由规则来产生的.这又分为面向资源和非面向资源两种产生链接的方法.比如 routes.rb文件中有两条 ...

  3. Python requests 为pfsense 添加Routes

    # !/usr/bin/python 2 # -*- coding: utf-8 -*- __author__ = "Evilxr" import requests ips = o ...

  4. Rails ---> routes.rb 详解

    理解路由的目的 看懂routes.rb文件中的代码 使用经典的hash风格或者现在比较流行的Restful风格构造你自己的路径 断定一个路径会映射到哪一个controller和action 路由的双重 ...

  5. [转]学习Nop中Routes的使用

    本文转自:http://www.cnblogs.com/miku/archive/2012/09/27/2706276.html 1. 映射路由 大型MVC项目为了扩展性,可维护性不能像一般项目在Gl ...

  6. play HTTP路由 http://play-framework.herokuapp.com/zh/routes#syntax

    HTTP路由 HTTP路由(译者注:Play的路径映射机制)组件负责将HTTP请求交给对应的action(一个控制器Controller的公共静态方法)处理. 对于MVC框架来说,一个HTTP请求可以 ...

  7. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

  8. Camel routes in Spring config file

    The normal spring bean definition configuration file, the xsi:schemaLocation only has two: beans and ...

  9. Howto add permanent static routes in Ubuntu

    Static routing is the term used to refer to the manual method used to set up routing. An administrat ...

随机推荐

  1. HTML5 Canvas实现刮刮卡效果实例

    HTML: <style> #canvas { border: 1px solid blue; position: absolute; left: 10px; top: 10px; bac ...

  2. java开发规范总结_命名规范

    规范需要平时编码过程中注意,是一个慢慢养成的好习惯 1.文件 1.属性文件后缀为properties,并且符合java中i18n的规范:   2.对于各产品模块自己的配置文件必须放置在自己模块的con ...

  3. Index Full Scan vs Index Fast Full Scan-1103

    [Oracle] Index Full Scan vs Index Fast Full Scan作者:汪海 (Wanghai) 日期:14-Aug-2005 出处:http://spaces.msn. ...

  4. iOS远程消息推送自我整理版

    @interface AppDelegate () <UIApplicationDelegate> @end @implementation AppDelegate - (BOOL)app ...

  5. 文字和表单(checkbox/radio)元素垂直对齐方法,兼容Firefox和IE。

    这几天在做表单时总会碰到复选框(checkbox)和单选框(radio)与文字不对齐的问题,要不是checkbox上浮了,要不是文字上浮.在前端开发过程中,单(复)选框和它们后面的提示文字在不进行任何 ...

  6. 静态方法块 static 以及对象属性&类属性的用法

    使用静态块的好处:只要在类被加载时,static块就会被调用,整个过程就调用这么一次,不会在后面的对象处又不断的调用.如果不使用它,就会出现如下问题:new一个对象,我就要调用一次所需的这些内容,重复 ...

  7. SGU 276 Andrew's Troubles

    简单的题.直接找题意来就好了. #include <iostream> #include <cstdio> using namespace std; int s, n, ans ...

  8. ICE学习第二步-----从第一个程序了解ICE(HelloWorld)

    ICE(Internet Communications Engine)是一种面向对象的中间件平台,主要用于网络通讯.它为面向对象的“客户端-服务器”模型的应用提供了一组很好的工具和API接口.目前在全 ...

  9. ecshop数据表

    ecs_account_log:账户变动日志(注册用户充值.支付等记录信息) ecs_ad:广告表 ecs_admin_action:管理员权限表(定义了128项功能操作) ecs_admin_log ...

  10. C#【数据库】 Excel打开到DataGridView

    if (openFileDialog1.ShowDialog() == DialogResult.OK) { Filename = openFileDialog1.FileName; string s ...