题意:

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 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条铁路,若两点之间无铁路相连,则建一条公路。从1到N点分别走两种路,若能保证汽车和火车在同一时刻不会在同一个点相遇(起始点除外),则输出两种路线都能到达N点的最小时刻。否则输出-1

分析:

两点之间只有一条边,所以无论怎么走都绝逼不可能中途相遇.......然后取两条最短路的较大值就可以了

代码:可以用一个矩阵临时存储一下输入的边

#include<bits/stdc++.h>
#include<queue>
using namespace std;
const int maxn=430;
const __int64 inf=(__int64)1<<62;//0x3f3f3f3f 太小了.... typedef pair<__int64,int> p;//<a,b> 起点到b的最短距离为a struct edge {int to;int cost;edge(int tto=0,int ccost=0):to(tto),cost(ccost){}
};int n,m;
int tmp[maxn][maxn];
__int64 d[maxn];
//int prev[maxn];
vector<edge> g[maxn];void dijkstra(int s) {priority_queue<p,vector<p>,greater<p> >qu;//堆按照p的first(最短距离)排序,小顶堆 fill(d,d+n+1,inf);
//  fill(prev,prev+n+1,-1);//+n 太小了...
//  memset(prev,sizeof(prev),-1);//混用会报错 d[s]=0;qu.push(p(0,s));//从起点出发到顶点s的最短距离为0while(!qu.empty()) {p temp=qu.top();qu.pop();int tmpv=temp.second;if(temp.first>d[tmpv]) continue;//跳过更新过程中入队的非最小值 for(int i=0;i<g[tmpv].size();++i) {//遍历该顶点连出的每条边 edge e=g[tmpv][i];if(d[e.to]>d[tmpv]+e.cost) {d[e.to]=d[tmpv]+(__int64)e.cost;
//              prev[e.to]=tmpv;qu.push(p(d[e.to],e.to));}}}
}//vector<int> getpath(int t) {//s -> t
//  vector<int> path;
//  while(t!=-1) {//从 t 倒着走,一直走到 s
//      path.push_back(t);
//      t=prev[t];
//  }
//  reverse(path.begin(),path.end());
//  return path;
//}int main() {//31 ms  8000 KBint a,b,c;scanf("%d%d",&n,&m);for(int i=0;i<=n;++i) {g[i].clear();}for(int i=1;i<=m;++i) {scanf("%d%d",&a,&b);tmp[a][b]=tmp[b][a]=1;}if(tmp[1][n]==1){//1和N铁路相连,走铁路最短路一定是1,只需要求公路的最短路 for(int i=1;i<=n;++i){for(int j=1;j<=n;++j){if(tmp[i][j]==0){g[i].push_back(edge(j,1));g[j].push_back(edge(i,1));                  }}} }else{for(int i=1;i<=n;++i){for(int j=1;j<=n;++j){if(tmp[i][j]==1){g[i].push_back(edge(j,1));g[j].push_back(edge(i,1));                 }}}         }dijkstra(1);if(d[n] == inf) puts("-1");else printf("%d\n",d[n]);return 0;
}

codefroces 601A 题意唬人的最短路相关推荐

  1. POJ3255 Roadblocks ——次短路

    Roadblocks 题意:求次短路 思路:见注释,用dijkstra同时维护最短路和次短路 // Decline is inevitable // Romance will last forever ...

  2. 洛谷1462 通往奥格瑞玛的道路 二分+spfa

    题目链接: https://www.luogu.org/problem/show?pid=1462 题意: 题解: 二分法+最短路判定. 二分经过城市的最大费用w,然后判定:对于每一个费用大于w的城市 ...

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

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

  4. 最短路 思维转换 POJ 3159需要深刻理解题意的模板题

    Candies Description: 在幼儿园的时候,Flymouse是班上的班长.有时班主任会给班上的孩子们带来一大袋糖果,让他们分发.所有的孩子都非常喜欢糖果,经常比较他们和别人买的糖果的数量 ...

  5. HDU1811 Rank of Tetris 拓扑排序+并查集 OR 差分约束最短路+并查集

    题目链接 题意:就是给你一堆关系,看能不能排出个确定的顺序 做法: 1. 拓扑排序+并查集 应该很容易想到的一种思路,大于小于建立单向边.对于相等的呢,就把他们缩成一个点.就用并查集缩成一个点就行了 ...

  6. NC19814最短路 LCA+bfs暴力

    700ms飘过 你可能不相信 我先加了inline 又加了快读... 然后TLE->AC 题意: 第一行两个整数n和m,表示图的点数和边数(1≤ n≤ 100000, 1≤ m≤ n+100). ...

  7. 图论刷水题记录(二)(最短路-----SPFA算法)

    继第一篇的后续,又来刷水题了,写的是SPFA算法,这个算法的复杂度比较玄学,感觉能不用就不用了,但是他的好处就是可以判断负圈. 3月26日: 1.POJ 1847 Tram 题意:在一个交通网络上有N ...

  8. 图论刷水题记录(一)(最短路-----dijkstra算法)

    最近实在不知道干些什么,感觉自己除了水题什么都不会做,算了去刷一刷图论的水题吧本来想合起来一起发,想了想太长的话以后看起来也不方便,题目所以今天晚上就先发了dij部分,由上到下由易变难. 1.POJ ...

  9. acwing单源最短路的建图模式总结

    .根据边权的范围以及问题求解的需要,最短路问题可以分为以下 4 种情形,分别用不同的算法求解. • 单源最短路径(固定一个顶点为原点,求源点到其他每个顶点 的最短路径) • 1. 边权非负:Dijks ...

最新文章

  1. .Net桌面程序的旗舰--参加亚控科技组态王7.0发布有感
  2. 口碑好的mysql数据监控平台_构建狂拽炫酷屌的 MySQL 监控平台
  3. synchronized(二)
  4. linux 中php以及nginx的重启命令
  5. Satori变种正在通过替换钱包地址盗取ETH数字代币
  6. android调用网页方法,Android调用手机浏览器的正确方式
  7. PMP考试技巧(必备)
  8. C/C++编程笔记:浅析 C 语言中宏定义的使用,知识点全解
  9. PyInstaller 生成exe文件
  10. ios开发笔记之 视频播放收藏
  11. HTML5植物大战僵尸游戏源码下载
  12. rtk手簿Android代码,中海达rtk手机测量软件(Hi-Survey Road)
  13. Educoder中Spark算子--java版本
  14. 如何学习(Java)
  15. Typora 未保存文件找回
  16. vnc好用吗,vnc是什么,vnc好用吗
  17. php菜单无限极分类
  18. 22.23.24.25.盒须图(boxplot)、棉棒图(Stem Plot; Lollipop plot)、极坐标图、雷达图(Radar Chart)
  19. 监控 prometheus及其部署及server discovery,alertmanager,grafana(更新结束)
  20. 淘宝天猫运营,天天特价活动规则、要求,商家攻略

热门文章

  1. 数据库之删除表的使用
  2. JS字符串单双字节长度判断
  3. (视频+源码)助力年后跳槽:对标阿里P8的大数据开发全套教程
  4. DataStage 分区(Partition)
  5. 一建经济科目哪个老师讲的好?
  6. Nginx使用$remote_addr获取不到用户真实IP解决
  7. Linux userdel
  8. 好佳居软装十大品牌 软装有较多知识你需要知道
  9. CAD_随机三维纤维插件 圆柱体纤维
  10. 电路与数字逻辑课程设计-电子钟功能