codeforces 601A The Two Routes(最短路 flody)
2016-04-07 19:19
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 townn (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.
Examples
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个城市,m条铁路,没有铁路的地方都是公路,火车只能走铁路,汽车只能走公路,现在要求火车汽车同时从1城市出发,问最短经过多长时间火车汽车同时到达n城市, 注意:在中间通过的城市汽车火车不能同时到达,但是他们都可以在某个城市停下休息任意时间;
题解:同时求出汽车火车的最短路输出大的一个即可(因为我们可以将较小的那个最短路认为它在起点等待较慢的那个车)
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 townn (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.
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条铁路,没有铁路的地方都是公路,火车只能走铁路,汽车只能走公路,现在要求火车汽车同时从1城市出发,问最短经过多长时间火车汽车同时到达n城市, 注意:在中间通过的城市汽车火车不能同时到达,但是他们都可以在某个城市停下休息任意时间;
题解:同时求出汽车火车的最短路输出大的一个即可(因为我们可以将较小的那个最短路认为它在起点等待较慢的那个车)
#include<stdio.h> #include<string.h> #include<vector> #include<map> #include<queue> #include<stack> #include<cstdio> #include<string> #include<math.h> #include<algorithm> #define LL long long #define PI atan(1.0)*4 #define DD double #define MAX 450 #define mod 10003 #define dian 1.000000011 #define INF 0x3f3f3f3f using namespace std; int ma[MAX][MAX],s[MAX][MAX]; //int dis[MAX][MAX],dis1[MAX][MAX]; int n,m; void floyd() { int i,j,k; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { ma[i][j]=min(ma[i][j],ma[i][k]+ma[k][j]); s[i][j]=min(s[i][j],s[i][k]+s[k][j]); } } } return ; } void init() { int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j) ma[i][j]=s[i][j]=0; else ma[i][j]=INF; } } } int main() { int i,j,a,b; while(scanf("%d%d",&n,&m)!=EOF) { init(); while(m--) { scanf("%d%d",&a,&b); ma[a][b]=ma[b][a]=1; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j) continue; if(ma[i][j]!=INF) s[i][j]=INF; else s[i][j]=1; } } floyd(); if(ma[1] !=INF&&s[1] !=INF) printf("%d\n",max(ma[1] ,s[1] )); else printf("-1\n"); } return 0; }
相关文章推荐
- find命令
- Java的继承
- 共同学习Java源码--常用数据类型--String(十)
- 脱离 Spring 实现复杂嵌套事务,之五(SUPPORTS - 跟随环境)
- hadoop 权威指南 2、3章读书笔记
- JavaScript之定义函数的方法
- M2
- 我的第一篇博客——红黑树的基本操作
- redis中5种数据类型介绍、命令(二)
- scikit-learn的主要模块和基本使用
- 【多重背包】HDU2191悼念512汶川大地震遇难同胞——珍惜现在,感恩生活【模板】
- [BZOJ2132]圈地计划(最小割)
- apache跨域问题
- 脱离 Spring 实现复杂嵌套事务,之三(REQUIRES_NEW - 独立事务)
- 脱离 Spring 实现复杂嵌套事务,之四(NESTED - 嵌套事务)
- C++实现——由年月日推算是星期几
- C# 网络编程之基于SMTP发送电子邮件
- Android官方开发文档Training系列课程中文版:通过NFC共享文件之发送文件到另一台设备
- 剑指offer-面试题2.实例Singleton模式
- [UOJ35]后缀排序