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 ...
随机推荐
- Qt之QTableView显示富文本
简述 对于QTableView中的显示,我们前面介绍过很多种,其中包括:文本.进度条.复选框等,今天我们介绍一下关于富文本的显示. 可能绝大多数小伙伴会通过QAbstractTableModel中的d ...
- 记一次高级java工程师职位的面试
阿里在业内做java方面的有关开发可谓是一流的.它给我的第一印象,就是办事效率很高. 周日简历发过去,周一电话就打过来了,接到电话后,面试官很客气,问现在方面吗,我说现在在上班,有点忙,然后和面试官约 ...
- css新增UI样式
1.圆角 border-radius <style> .box{width:200px;height:300px;border:1px solid #000;border-radius:1 ...
- hadoop数据容易出现错误的地方
最近在搞关于数据分析的项目,做了一点总结. 下图是系统的数据流向.容易出现错误的地方.1.数据进入hadoop仓库有四种来源,这四种是最基本的数据,简称ods,original data source ...
- SkinPP for VC
1.下载文件:SkinPPWTL.h,SkinPPWTL.dll,SkinPPWTL.lib以及Skin++皮肤库: 2.新建一个工程,如:基于多文档的工程,名为:MySkin: 3.将下载的Skin ...
- Word排版成树形结构技巧
初始文字 A A1 A2 B1 B1 B2 C C1 希望效果 关健设置
- linux shell中的 #!/bin/bash
#!/bin/bash是指此脚本使用/bin/bash来解释执行. 其中,#!是一个特殊的表示符,其后,跟着解释此脚本的shell路径. bash只是shell的一种,还有很多其它shell,如:sh ...
- php 5.3开始使用mysqlnd作为的默认mysql访问驱动
mysqlnd成为php 5.3中的默认mysql驱动,它有如下优点: mysqlnd更容易编译: 因为它是php源码树的一个组成部分 mysqlnd和php内部机制结合更紧密,是优化过的mysql驱 ...
- myeclipse 打开xml jsp页面慢 有时候会自动退出
Myeclipse默认打开文件的方式是 jsp design,每次双击或者使用Ctrl+Shift+R打开 就会用这个打开 ,太慢了而且多次导致Myeclipse挂掉.可以通过以下的方式转化成你想要的 ...
- 错误记录:html隐藏域的值存字符串时出错
问题 webform在后台给前台传值. <input type="hidden" value="<%=userType %>" id=&qu ...