CodeForces 601A The Two Routes(dijkstra最短路)——Codeforces Beta Round #333 (Div.1 Div. 2)
2015-11-27 12:39
507 查看
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 ≤ 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 test(s)
input
output
input
output
input
output
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个城市(记为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不会的可以网上学习一下
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 ≤ 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 test(s)
input
4 2 1 3 3 4
output
2
input
4 6
1 21 31 4
2 32 4
3 4
output
-1
input
5 5
4 23 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个城市(记为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-4 using namespace std; const int N = 405; const int M = 100000; const int inf = 100000000; const int mod = 2009; int s ,d ; bool w ; 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 ; 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 ; if(sum1!=inf&&sum2!=inf) printf("%d\n",max(sum1,sum2)); else puts("-1"); return 0; }菜鸟成长记
相关文章推荐
- CodeForces 594D REQ(树状数组)
- 关于“一秒一G LiFi比WiFi快一百倍”的评论
- MYSQL数据库的存储过程(Stored Procedure)
- 1064. Complete Binary Search Tree (30)
- 自定义dialog视图
- 地表两个点 的 距离计算公式
- 1064. Complete Binary Search Tree (30)
- React 入门实例教程
- MYSQL数据库的事务(Transaction)
- 安卓游戏开发者:我是如何把包体减了88%
- 232. Implement Queue using Stacks
- dex分包变形记
- 修改PullToRefresh下拉刷新动画
- MYSQL数据库的游标(cursor)
- 一个优质的新闻客户端app
- 如何用Python写一个贪吃蛇AI
- Oracle索引重建
- 【NCRE】——c#判断ppt中的艺术字
- 喜欢用战术上的勤奋去敷衍自己战略上的懒惰——做客服,发货,这两个工作是没有意义的——你懂了怎么发货吗?懂了怎么打包吗?懂了怎么做客服吗?(淘宝的黄金时代已经过去了——做单一产品就是要走量)
- CSS问题:background-image:url(路径问题)