Description

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

Sample Input

Input

Output

Input

Output
-
Input Output

题意:

在北极,有n个城镇(编号从1到n)和m个双向铁路。这个城镇有一个简单的道路网络 - 对于每一对不同的城镇x和y来说,当且仅当它们之间没有铁路时候,在x,y两镇之间必定有一条双向的公路。

从任意一个小镇走一条铁路或一条公路到一个不同的小镇需要一个小时。火车和巴士同时离开城镇1。他们两个都有相同的目的地n,他们并没有在路上停下来(但他们可以在城镇n等待)。火车只能沿着铁路行驶,公共汽车只能沿着公路行驶。

你需要规划出火车和巴士的路线;

每条路线可以多次使用任何公路或者铁路。要考虑的最重要的方面之一是安全 - 为了避免在铁路口岸发生事故,火车和公共汽车不能同时到达同一个镇(n镇除外)。在这些限制下,两辆车到达城镇n所需的最少小时数中较大的那个(巴士和火车到达时间的最大值)是多少?请注意,巴士和火车不需要在同一时间到达城镇n,但是可以同时到达。

思路:

这个题真的一句话都不能丢。其中很重要的一句话是      当且仅当它们之间没有铁路时候,在x,y两镇之间必定有一条双向的公路。也就是说起点和终点一定会有一条路,要么是铁路,要么是公路。

如果是有铁路直连,那么直接求公路的起点到终点的最短路。如果是有公路直连,那么直接求铁路的起点到终点的最短路。就是一个简单的最短路的题目!

代码:

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie();
using namespace std;
#define inf 0x3f3f3f3f
int map1[][];
int map2[][];
int dis[],visit[];
int n,m;
int dijstra(int map1[][])
{
int i,j,pos=,min,sum=;
memset(visit,,sizeof(visit));
for(i=; i<=n; ++i)
{
dis[i]=map1[][i];
}
visit[]=;
dis[]=;
for(i=; i<n; i++)
{
min=inf;
for(j=; j<=n; ++j)
{
if(visit[j]==&&min>dis[j])
{
min=dis[j];
pos=j;
}
}
visit[pos]=;
for(j=; j<=n; ++j)
{
if(visit[j]==&&dis[j]>dis[pos]+map1[pos][j])
dis[j]=dis[pos]+map1[pos][j];
}
}
return dis[n];
}
int main()
{
//IO;
int i,j;
while(~scanf("%d%d",&n,&m))
{
for(i=; i<=n; ++i)
{
for(j=; j<=n; ++j)
{
map1[i][j]=inf;
map2[i][j]=inf;
}
}
int a,b,c;
for(i=; i<=m; ++i)
{
scanf("%d%d",&a,&b);
c=;
map1[a][b]=map1[b][a]=c;
}
for(i=; i<=n; ++i)
{
for(j=; j<=n; ++j)
{
if(map1[i][j]==inf)
map2[i][j]=;
}
}
int count=;
if(map1[][n]==||map1[n][]==)
count=dijstra(map2);
else
count=dijstra(map1);
if(count==inf)
printf("-1\n");
else
printf("%d\n",count);
}
return ;
}

CodeForces 602C The Two Routes(最短路)的更多相关文章

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

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

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

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

  3. 【50.00%】【codeforces 602C】The Two Routes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. codeforces 601A The Two Routes(最短路 flody)

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

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

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

  6. 【CodeForces 602C】H - Approximating a Constant Range(dijk)

    Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...

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

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

  8. Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  9. Shortest Path Codeforces - 59E || 洛谷P1811 最短路_NOI导刊2011提高(01)

    https://codeforces.com/contest/59/problem/E 原来以为不会..看了题解发现貌似自己其实是会的? 就是拆点最短路..拆成n^2个点,每个点用(i,j)表示,表示 ...

随机推荐

  1. ArrayList既然继承自AbstractList抽象类,而AbstractList已经实现了List接口,那么ArrayList类为何还要再实现List接口呢?

    https://www.cnblogs.com/bluejavababy/p/4320545.html

  2. 【CodeForces】578 C. Weakness and Poorness

    [题目]C. Weakness and Poorness [题意]给定含n个整数的序列ai,定义新序列为ai-x,要使新序列的最大子段和绝对值最小,求实数x.n<=2*10^5. [算法]二分| ...

  3. [网站安全] [实战分享]WEB漏洞挖掘的一些经验分享

    WEB漏洞有很多种,比如SQL注入,比如XSS,比如文件包含,比如越权访问查看,比如目录遍历等等等等,漏洞带来的危害有很多,信息泄露,文件上传到GETSHELL,一直到内网渗透,这里我想分享的最主要的 ...

  4. Android Service使用简单介绍

    作为一个android初学者,经常对service的使用感到困惑.今天结合Google API 对Service这四大组件之一,进行简单使用说明. 希望对和我一样的初学者有帮助,如有不对的地方,也希望 ...

  5. Django 1.10中文文档-第一个应用Part7-自定义管理站点

    开发第一个Django应用,Part7 本教程上接Part6.将继续完成这个投票应用,本节将着重讲解如果用Django自动生成后台管理网站. 自定义管理表单 通过admin.site.register ...

  6. 保护眼睛(改变窗口颜色和Pdf背景颜色)

    保护眼睛(改变窗口颜色和Pdf背景颜色) 昨天用了一个好朋友告诉我的保护眼睛的方法,效果很不错哦-- 今天告诉大家,一起爱护偶们明亮的眼睛吧!!!       首先需要改一下设置,如果常常用电脑很容易 ...

  7. 监控MYSQL主从同步配置中监控从库运行状态的脚本

    代码如下: #!/bin/bash #Check MySQL Slave's Runnning Status #Crontab time 00:10 MYSQLPORT=`netstat -na|gr ...

  8. python是如何进行内存管理的?

    Python内存管理机制 Python内存管理机制主要包括以下三个方面: 引用计数机制 垃圾回收机制 内存池机制 引用计数 举个例子说明引用是什么: 1 如上为一个简单的赋值语句,1就是对象,a就是引 ...

  9. Tutorial 4: Authentication & Permissions

    转载自:http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/ Tutorial 4: Auth ...

  10. 数据分析python应用到的ggplot(二)

    还是优达学院的第七课 数据:https://s3.amazonaws.com/content.udacity-data.com/courses/ud359/hr_by_team_year_sf_la. ...