codeforces 601A The Two Routes(最短路 flody)
2 seconds
256 megabytes
standard input
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.
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 2
1 3
3 4
2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
-1
5 5
4 2
3 5
4 5
5 1
1 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个城市,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][n]!=INF&&s[1][n]!=INF)
printf("%d\n",max(ma[1][n],s[1][n]));
else
printf("-1\n");
}
return 0;
}
codeforces 601A The Two Routes(最短路 flody)的更多相关文章
- [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路
14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...
- ACM学习历程—CodeForces 601A The Two Routes(最短路)
题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...
- CodeForces - 601A The Two Routes
http://codeforces.com/problemset/problem/601/A 这道题没想过来, 有点脑筋急转弯的感觉了 本质上就是找最短路径 但是卡在不能重复走同一个点 ----> ...
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
- The Two Routes CodeForces - 601A(水最短路)
一个完全图 1和n肯定有一条路 不是公路就是铁路 另= 另一个跑遍最短路即可 #include <bits/stdc++.h> #define mem(a, b) memset(a, ...
- CodeForces 602C The Two Routes(最短路)
Description In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. T ...
- Codeforces 601A:The Two Routes 宽搜最短路径
A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)
解题报告 农场有k个挤奶机和c头牛,每头牛到每一台挤奶机距离不一样,每台挤奶机每天最多挤m头牛的奶. 寻找一个方案,安排每头牛到某一挤奶机挤奶,使得c头牛须要走的全部路程中的最大路程的最小值. 要使每 ...
- Codeforces 545E. Paths and Trees 最短路
E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...
随机推荐
- Mvc 自带分页控件PagedList.Mvc Demo示例
添加/下载PagedList.Mvc 直接搜索mvc pagelist 就会出来.安装完成即可.在项目的packages文件夹下面就会出现PagedList.Mvc.4.5.0.0 和PagedLis ...
- LA 3695 Distant Galaxy
给出n个点的坐标(坐标均为正数),求最多有多少点能同在一个矩形的边界上. 题解里是构造了这样的几个数组,图中表示的很明白了. 首先枚举两条水平线,然后left[i]表示竖线i左边位于水平线上的点,on ...
- IOS中控制器的重要方法使用
1.屏幕即将旋转的时候调用(控制器监控屏幕旋转) - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfac ...
- spring.net IOC容器
spring.net 通过配置文件的方式 帮我们实现了IoC功能,实现方式非常灵活,且多种多样. 点击查看 创建对象 我们只需定义接口和实现方法,spring.net帮我们实现了其他功能. 第一步,定 ...
- TCP握手
1.TCP的三次握手四次挥手 第一次握手:Client将标志位SYN置为1,随机产生一个值seq=J,并将该数据包发送给Server,Client进入SYN_SENT状态,等待Server确认. 第二 ...
- 【转】Eclipse常用快捷键
原文网址:http://denver.blog.51cto.com/272871/52219 编辑相关快捷键 1.[Alt+/] 为用户提供内容的辅助. 2.[Ctrl+O] 显示类中方法和属性的大纲 ...
- 配置Linux 11G R2 RAC NTP服务
安装Oracle 11g RAC时,我们需要配置ntp服务.在使用虚拟机的情况下对于时钟同步方式的配置有很多种方式,可以使用vmware自带的时钟同步功能,也可以直接将本地的一个节点用作时间服务器.本 ...
- Arduino中的数据类型范围
注意int不是4字节而仅仅是2字节!!! int: -32,768 ~ 32,767 (2字节) long: 4字节 http://www.arduino.cc/en/Reference/Int
- 使用Drush管理Drupal站点
Drush(Drush = Drupal + Shell)就是使用命令行命令来操作Drupal站点,它的命令格式与git类似,都是双字命令(drush + 实际的命令).既然是命令行命令,也就可以使用 ...
- mac配置appium进行自动化测试
先去brew.sh这个网站,拷贝最新的下载安装命令,黏贴到终端里,回车,按提示下载安装. 安装完毕后,运行brewupdate 看看是否有更新,然后输入brew ios-webkit-debug-pr ...