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. 移动端touchstar、touchmove、touchend 事件如果页面有滚动时不让触发 touchend 事件。

    /*仅适用于内容中点击元素.对于拖动等元素,需要自行在页面处理. * 主要是绑定touchstart和touchmove事件,并判断用户按下之后手指移动了多少像素. * 如果手指移动距离小于10像素, ...

  2. 第七篇、Nginx Install On Mac

    方式一: 在mac上安装nginx,依次安装对应的依赖 pcre ./configure --prefix=/usr/local/pcre-8.37 --libdir=/usr/local/lib/p ...

  3. PHP~foreach遍历名单数组~有必要多次观看练习

  4. 用jQuery实现瀑布流效果学习笔记

    jQuery一直没系统的学,只知道是js库,封装了好多js函数,方便了开发.以前做过一个原生的图片网站瀑布流效果,超级麻烦,这次用了jQuery方法,瞬间代码浓缩了,只有56行js代码.神奇的让我来把 ...

  5. html5 DeviceOrientation来实现手机网站上的摇一摇功能

    原文地址:http://www.cootm.com/?p=706 从网上转载看到的,感觉不错,就转过来了,特此感谢 cnblogs 的 幸福2胖纸的码农生活,直接转载了,不要介意!呵呵 以下是转载内容 ...

  6. atoi的实现

    #include <iostream> using namespace std; int atoi(char* s) { int retval=0; int n=1; if (*s=='- ...

  7. 那些年被我坑过的Python——第十章Broker(rabbitMQ/redis)

    基于RabbitMQ的direct任务驱动异步RPC程序实现: RPC_dispatcher指令分发器: #!/usr/bin/env python # -*- coding:utf-8 -*- __ ...

  8. 常用Firefox扩展

    最近思维混乱,无心做事,故整理下东西.(PS:有些是firefox自带的.) 1.标签页管理器 2.1.41 用途:在新标签页打开书签.历史.地址.搜索. 主页:http://www.firefox. ...

  9. E题 - A+B for Input-Output Practice (IV)

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description You ...

  10. Extjs通过structs2生成树结构

    Extjs为我们提供了强大的树的生成方式,我们不必通过原始的js去生成树形结构.在这里我做了一个简单的树结构生成.代码如下,同时在后台使用了fastjson-1.1.15.jar的jar包生成json ...