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 townn (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 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条铁路,没有铁路的地方都是公路,火车只能走铁路,汽车只能走公路,现在要求火车汽车同时从1城市出发,问最短经过多长时间火车汽车同时到达n城市,   注意:在中间通过的城市汽车火车不能同时到达,但是他们都可以在某个城市停下休息任意时间;

题解:同时求出汽车火车的最短路输出大的一个即可(因为我们可以将较小的那个最短路认为它在起点等待较慢的那个车)

#include<stdio.h>
#include<string.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 450
#define mod 10003
#define dian 1.000000011
#define INF 0x3f3f3f3f
using namespace std;
int ma[MAX][MAX],s[MAX][MAX];
//int dis[MAX][MAX],dis1[MAX][MAX];
int n,m;
void floyd()
{
int i,j,k;
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
ma[i][j]=min(ma[i][j],ma[i][k]+ma[k][j]);
s[i][j]=min(s[i][j],s[i][k]+s[k][j]);
}
}
}
return ;
}
void init()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
ma[i][j]=s[i][j]=0;
else
ma[i][j]=INF;
}
}
}
int main()
{
int i,j,a,b;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
while(m--)
{
scanf("%d%d",&a,&b);
ma[a][b]=ma[b][a]=1;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j) continue;
if(ma[i][j]!=INF)
s[i][j]=INF;
else
s[i][j]=1;
}
}
floyd();
if(ma[1][n]!=INF&&s[1][n]!=INF)
printf("%d\n",max(ma[1][n],s[1][n]));
else
printf("-1\n");
}
return 0;
}

  

codeforces 601A The Two Routes(最短路 flody)的更多相关文章

  1. [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路

    14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...

  2. ACM学习历程—CodeForces 601A The Two Routes(最短路)

    题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...

  3. CodeForces - 601A The Two Routes

    http://codeforces.com/problemset/problem/601/A 这道题没想过来, 有点脑筋急转弯的感觉了 本质上就是找最短路径 但是卡在不能重复走同一个点 ----> ...

  4. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  5. The Two Routes CodeForces - 601A(水最短路)

    一个完全图 1和n肯定有一条路  不是公路就是铁路  另= 另一个跑遍最短路即可 #include <bits/stdc++.h> #define mem(a, b) memset(a, ...

  6. CodeForces 602C The Two Routes(最短路)

    Description In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. T ...

  7. Codeforces 601A:The Two Routes 宽搜最短路径

    A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)

    解题报告 农场有k个挤奶机和c头牛,每头牛到每一台挤奶机距离不一样,每台挤奶机每天最多挤m头牛的奶. 寻找一个方案,安排每头牛到某一挤奶机挤奶,使得c头牛须要走的全部路程中的最大路程的最小值. 要使每 ...

  9. Codeforces 545E. Paths and Trees 最短路

    E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...

随机推荐

  1. 瞎折腾之 Lucene.Net + MVC 搜索功能(上)

    前言 首先,关于Lucene.Net 的文章已经很多了.我这次决定写出来只是为了练练手,虽然在别人看来没什么用,但是自己确实是手动实践了一把.我个人觉得还是有意义的.爱折腾.敢于实践.才能有所收获,才 ...

  2. ASP.NET MVC 实现二级域名

      自从微软发布 ASP.NET MVC 和routing engine (System.Web.Routing)以来,就设法让我们明白你完全能控制URL和routing,只要与你的applicati ...

  3. [Swift系列]002-基础语法

    基础语法就那老几样,很快可以说完 [常量.变量] 1.变量用 var,系统自动去判断类型,但变量再次赋值需保持数据类型一致 var  a=50 相信用过js/java/C#的,对这个var都不陌生 使 ...

  4. DataTable反向模糊匹配查找语法

    正向写法: string filter = "code like '%"+sheetname+"%'"; filter值为: code like '%表F.3_ ...

  5. 【Android】Handler使用入门

    本讲内容:Handler使用入门 当用户点击一个按钮时如果执行的是一个常耗时操作的话,处理不好会导致系统假死,用户体验很差,而Android则更进一步,如果任意一个Acitivity没有响应5秒钟以上 ...

  6. JAVA遍历一个文件夹中的所有文件

    在实际项目中给定一文件夹,得到这个文件夹下所有的文件这样的需求并不是很多,更多的是查找或是删除某一具体的文件 import java.io.File; import java.util.ArrayLi ...

  7. JVM参数汇总

    一.java启动参数共分为三类: 其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容:其二是非标准参数(-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现都满足 ...

  8. Android 动画深入解析

    http://blog.csdn.net/rain_butterfly/article/details/39642613

  9. js 判断页面是否加载完成

    javascript代码如下: document.onreadystatechange = subSomething; //当页面加载状态改变的时候执行这个方法 function subSomethi ...

  10. MFC中TRACE

    错误 1 error C1189: #error :  Building MFC application with /MD[d] (CRT dll version) requires MFC shar ...