Codeforces--333C--The Two Routes(最短路弗洛伊德)(思维)
来源:互联网 发布:大数据技术 原理 编辑:程序博客网 时间:2024/05/23 14:32
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 ≤ n, u ≠ 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
4 21 33 4
2
4 61 21 31 42 32 43 4
-1
5 54 23 54 55 11 2
3
Hint
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.
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define INF 0x3f3f3f3fint n,m,map1[1010][1010],map2[1010][1010];int main(){while(scanf("%d%d",&n,&m)!=EOF){for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){map1[i][j]=map1[j][i]=INF;map2[i][j]=map2[j][i]=1;}int x,y;for(int i=0;i<m;i++){scanf("%d%d",&x,&y);map1[x][y]=map1[y][x]=1;map2[x][y]=map2[y][x]=INF;}for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)map1[i][j]=min(map1[i][j],map1[i][k]+map1[k][j]);for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)map2[i][j]=min(map2[i][j],map2[i][k]+map2[k][j]);if(map1[1][n]>=INF||map2[1][n]>=INF)printf("-1\n");else{int maxx=max(map1[1][n],map2[1][n]);printf("%d\n",maxx);}}return 0;}
- Codeforces--333C--The Two Routes(最短路弗洛伊德)(思维)
- Codeforces 602C The Two Routes【最短路+思维】
- CodeForces 602C The Two Routes(最短路)
- Codeforces Round #333 (Div. 2)C. The Two Routes(最短路)
- Codeforces Round #333 (Div. 2) C. The Two Routes (最短路)
- Codeforces #333 (Div. 1) A. The Two Routes(最短路)
- CF#333(Div2) C. The Two Routes(最短路)
- Codeforces Round #333 (Div. 2) 602C The Two Routes 最短路
- Codeforces 601A The Two Routes【最短路】
- codeforces 601 A. The Two Routes (最短路d+f)
- 【Codeforces Round 333 (Div 2)C】【最短路】The Two Routes 完全图两种双向边的最小最大距离
- CodeForces 601A The Two Routes(最短路径)
- CodeForces 601A The Two Routes(dijkstra最短路)——Codeforces Beta Round #333 (Div.1 Div. 2)
- Codeforces Round #333 (Div. 2)-C The Two Routes(SPFA)
- Codeforces Round #333 (Div. 2) C. The Two Routes
- Codeforces Round #333 (Div. 2) C. The Two Routes
- Codeforces Round #333 (Div. 2) C. The Two Routes
- 【50.00%】【codeforces 602C】The Two Routes
- 常用语言和工具的中文翻译
- OpenCV学习笔记(四)—— OpenCV for Android移植到Android平台
- 安卓探究ListView+Adapter数据在工作线程中更新引发的crash以及解决方法(三)
- cocos2d 使用 C++开发游戏 出现#include "CardSprite.h" 无法引入源文件的情况。
- JFinal实现原理
- Codeforces--333C--The Two Routes(最短路弗洛伊德)(思维)
- ios开发多线程篇——多线程简单介绍
- android多点触控统一的原理(使用 event.getAction()&MotionEvent.ACTION_MASK的原因)
- ubuntu on power服务器上安装docker binary package
- TokuMX will not run with transparent huge pages enabled.
- GLFW Keyboard keys
- iOS开发者账号总结
- Android中PopupWindow使用详解
- 171. Excel Sheet Column Number