CodeForces 601A The Two Routes(dijkstra最短路)——Codeforces Beta Round #333 (Div.1 Div. 2)
来源:互联网 发布:图片来源于网络 编辑:程序博客网 时间:2024/05/17 07:13
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.
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 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.
4 21 33 4
2
4 61 21 31 42 32 43 4
-1
5 54 23 54 55 11 2
3
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个城市(记为1~n),我们要从城市1出发到城市n,有两张交通方式,火车or汽车,给你m条双向铁路(即城市u->城市v和城市v->城市u皆可),即只有火车能行驶,未给出铁路的两城市之间均有公路连通,现知道不管哪种交通方式,相连通的两城市均需要1小时的时间到达,问到达城市n所需的最少时间为多少(即min(maxi(火车,汽车))),若无法达到,则输出"-1"
解题思路:由于n比较小,所以我们可以采用O(n^2)的Dijkstra,可以先求出火车从城市1到达城市n的最短时间,记录下来之后,处理一下各城市之间的时间矩阵(原先火车无法到达的改为1,原先可以达到的改为inf),然后再求一遍最短时间,取两种交通方式的较大值即可,Dijkstra不会的可以网上学习一下
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<cmath>#include<string>#include<algorithm>#include<iostream>#define exp 1e-4using namespace std;const int N = 405;const int M = 100000;const int inf = 100000000;const int mod = 2009;int s[N][N],d[N];bool w[N];int main(){ int n,m,i,j,k,u,v,Min,sum1,sum2; memset(w,false,sizeof(w)); scanf("%d%d",&n,&m); for(i=1;i<=n;i++) for(j=1;j<=n;j++) s[i][j]=inf; for(i=0;i<m;i++) { scanf("%d%d",&u,&v); s[u][v]=s[v][u]=1; } d[1]=sum1=sum2=0; w[1]=true; for(i=2;i<=n;i++) d[i]=s[1][i]; for(i=1;i<n;i++) { Min=inf; for(j=1;j<=n;j++) if(!w[j]&&d[j]<Min) Min=d[k=j]; if(Min==inf) break; w[k]=true;; for(j=1;j<=n;j++) d[j]=min(d[j],d[k]+s[k][j]); } sum1=d[n]; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(s[i][j]!=inf) s[i][j]=inf; else s[i][j]=1; memset(w,false,sizeof(w)); d[1]=0; w[1]=true; for(i=2;i<=n;i++) d[i]=s[1][i]; for(i=1;i<n;i++) { Min=inf; for(j=1;j<=n;j++) if(!w[j]&&d[j]<Min) Min=d[k=j]; if(Min==inf) break; w[k]=true;; for(j=1;j<=n;j++) d[j]=min(d[j],d[k]+s[k][j]); } sum2=d[n]; if(sum1!=inf&&sum2!=inf) printf("%d\n",max(sum1,sum2)); else puts("-1"); return 0;}菜鸟成长记
- CodeForces 601A The Two Routes(dijkstra最短路)——Codeforces Beta Round #333 (Div.1 Div. 2)
- coderforce Codeforces Round #333 (Div. 1) A. The Two Routes
- Codeforces Round #333 (Div. 2)C. The Two Routes(最短路)
- Codeforces Round #333 (Div. 2) 602C The Two Routes 最短路
- Codeforces Round #333 (Div. 2) C. The Two Routes (最短路)
- Codeforces #333 (Div. 1) A. The Two Routes(最短路)
- 【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
- Codeforces Round #333 (Div. 2) C. 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(SPFA)
- CodeForces 581A Vasya the Hipster(简单题)——Codeforces Beta Round #322 (Div. 2)
- Codeforces Beta Round #18 (Div. 2 Only)——A
- Codeforces Beta Round #49 (Div. 2)—A
- CodeForces 602A Two Bases(简单题,比较两个不同进制数的大小)——Codeforces Beta Round #333 (Div. 2)
- 【codeforces】601A—— The Two Routes
- IE下载文件名乱码的解决方法
- JPA:双向一对一
- Linux防火墙设置
- Service
- phpstorm+xdebug调试+chrome装xdebug
- CodeForces 601A The Two Routes(dijkstra最短路)——Codeforces Beta Round #333 (Div.1 Div. 2)
- 成功的背后!(给所有IT人)
- 通达OA如何把信息中心添加到目录菜单
- POJ 3080 Blue jeans
- 解决unity中Animation Event无法正确保存的问题
- 绕过wed防火墙asp脚本
- 全局获取Context的技巧
- [Android]修改bitmap大小
- ubuntu下常用工具