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 ...
随机推荐
- C语言中的三值合一
在学习C语言中我们会发现这样一种情况: #include<stdio.h> Int main() { Int ar[10]; printf(“%p\n”,ar); printf(“%p\n ...
- WEBUS2.0 In Action - 搜索操作指南 - (1)
上一篇:WEBUS2.0 In Action - 索引操作指南(2) | 下一篇:WEBUS2.0 In Action - 搜索操作指南(2) 1. IQueriable中内置的搜索功能 在Webus ...
- cURL: PHP并发处理方式
function classic_curl($urls, $delay) { $queue = curl_multi_init(); $map = array(); foreach ($urls as ...
- ecshop 在首页每个商品下显示已销售数量
1.在includes/lib_goods.php文件末尾加入以下代码 function get_buy_sum($goods_id) { $sql = "select sum(goods_ ...
- Linux/Unix shell 脚本中调用SQL,RMAN脚本
Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...
- OpenGL学习之路(二)
1 引子 在上一篇读书笔记中,我们对书本中给出的例子进行详细的分析.首先是搭出一个框架:然后填充初始化函数,在初始化函数中向OpenGL提供顶点信息(缓冲区对象)和顶点属性信息(顶点数组对象),并启用 ...
- suse linux环境变量设置
以在suse上安装jdk1.5为例说明: 安装jdk1.5完毕后,就可以配置环境变量了. su root XXXXXX // 键入管理员密码 对于suse来说,只需在/etc/profile 文件后 ...
- tomcat Manger App
转发链接,嘿嘿http://simeon.blog.51cto.com/18680/58877
- [搜片神器]直接从DHT网络下载BT种子的方法
DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr DHT系 ...
- 用正则表达式在注册页面(js/aspx.cs)的验证
1.验证邮箱(用户名) JS页面中: 首先定义变量和正则 var usermail = $("#usermail" ).val(); var username= /^([a-zA- ...