CodeForces 602C The Two Routes(最短路)
Description
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 yif 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 Input
Input Output Input Output
-
Input Output
题意:
在北极,有n个城镇(编号从1到n)和m个双向铁路。这个城镇有一个简单的道路网络 - 对于每一对不同的城镇x和y来说,当且仅当它们之间没有铁路时候,在x,y两镇之间必定有一条双向的公路。
从任意一个小镇走一条铁路或一条公路到一个不同的小镇需要一个小时。火车和巴士同时离开城镇1。他们两个都有相同的目的地n,他们并没有在路上停下来(但他们可以在城镇n等待)。火车只能沿着铁路行驶,公共汽车只能沿着公路行驶。
你需要规划出火车和巴士的路线;
每条路线可以多次使用任何公路或者铁路。要考虑的最重要的方面之一是安全 - 为了避免在铁路口岸发生事故,火车和公共汽车不能同时到达同一个镇(n镇除外)。在这些限制下,两辆车到达城镇n所需的最少小时数中较大的那个(巴士和火车到达时间的最大值)是多少?请注意,巴士和火车不需要在同一时间到达城镇n,但是可以同时到达。
思路:
这个题真的一句话都不能丢。其中很重要的一句话是 当且仅当它们之间没有铁路时候,在x,y两镇之间必定有一条双向的公路。也就是说起点和终点一定会有一条路,要么是铁路,要么是公路。
如果是有铁路直连,那么直接求公路的起点到终点的最短路。如果是有公路直连,那么直接求铁路的起点到终点的最短路。就是一个简单的最短路的题目!
代码:
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie();
using namespace std;
#define inf 0x3f3f3f3f
int map1[][];
int map2[][];
int dis[],visit[];
int n,m;
int dijstra(int map1[][])
{
int i,j,pos=,min,sum=;
memset(visit,,sizeof(visit));
for(i=; i<=n; ++i)
{
dis[i]=map1[][i];
}
visit[]=;
dis[]=;
for(i=; i<n; i++)
{
min=inf;
for(j=; j<=n; ++j)
{
if(visit[j]==&&min>dis[j])
{
min=dis[j];
pos=j;
}
}
visit[pos]=;
for(j=; j<=n; ++j)
{
if(visit[j]==&&dis[j]>dis[pos]+map1[pos][j])
dis[j]=dis[pos]+map1[pos][j];
}
}
return dis[n];
}
int main()
{
//IO;
int i,j;
while(~scanf("%d%d",&n,&m))
{
for(i=; i<=n; ++i)
{
for(j=; j<=n; ++j)
{
map1[i][j]=inf;
map2[i][j]=inf;
}
}
int a,b,c;
for(i=; i<=m; ++i)
{
scanf("%d%d",&a,&b);
c=;
map1[a][b]=map1[b][a]=c;
}
for(i=; i<=n; ++i)
{
for(j=; j<=n; ++j)
{
if(map1[i][j]==inf)
map2[i][j]=;
}
}
int count=;
if(map1[][n]==||map1[n][]==)
count=dijstra(map2);
else
count=dijstra(map1);
if(count==inf)
printf("-1\n");
else
printf("%d\n",count);
}
return ;
}
CodeForces 602C The Two Routes(最短路)的更多相关文章
- [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路
14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
- 【50.00%】【codeforces 602C】The Two Routes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 601A The Two Routes(最短路 flody)
A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- ACM学习历程—CodeForces 601A The Two Routes(最短路)
题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...
- 【CodeForces 602C】H - Approximating a Constant Range(dijk)
Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...
- Codeforces 545E. Paths and Trees 最短路
E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...
- Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- Shortest Path Codeforces - 59E || 洛谷P1811 最短路_NOI导刊2011提高(01)
https://codeforces.com/contest/59/problem/E 原来以为不会..看了题解发现貌似自己其实是会的? 就是拆点最短路..拆成n^2个点,每个点用(i,j)表示,表示 ...
随机推荐
- Stat3—因子分析(Factor Analysis)
题注:主成分分析分析与因子分析也有不同,主成分分析仅仅是变量变换,而因子分析需要构造因子模型.主成分分析:原始变量的线性组合表示新的综合变量,即主成分:因子分析:潜在的假想变量和随机影响变量的线性组合 ...
- 从urllib和urllib2基础到一个简单抓取网页图片的小爬虫
urllib最常用的两大功能(个人理解urllib用于辅助urllib2) 1.urllib.urlopen() 2. urllib.urlencode() #适当的编码,可用于后面的post提交 ...
- Oracle笔记之用户管理
1. 创建用户 创建用户使用create user语句,需要DBA权限: CREATE USER tom IDENTIFIED BY mot; 2. 更改用户密码 修改别人的密码需要DBA权限,或者a ...
- Solaris 系统命令使用说明
1. 查看进程 -- pgreproot@UA4300D-spa:~# pgrep fmd133095root@UA4300D-spa:~# pgrep -l fmd133095 fmdroot@ ...
- WebStorm 2016激活
最近在网上找到一个激活webStorm 的好东西.博主说对jetbrains下的所有产品都是可以用这种方式激活的...如:PhpStorm,IntelliJ JDEA等. 但别的产品我没有试过.对于w ...
- 用户空间与内核空间数据交换的方式(9)------netlink【转】
转自:http://www.cnblogs.com/hoys/archive/2011/04/10/2011722.html Netlink 是一种特殊的 socket,它是 Linux 所特有的,类 ...
- python操作adb代码
adb命令的练习 #!/usr/bin/env python # encoding: utf-8 import os import re nameplt=re.compile("packag ...
- tomcat+ngnix单机搭建集群及端口占用问题
1.将tomcat复制两份,如下: 2.新建环境变量,如下: 3.修改其中一个的配置文件,另一个保持不变,修改server.xml配置文件如下: <Server port="9005& ...
- 大数据系列之kafka-java实现
Java源码GitBub地址: https://github.com/fzmeng/kafka-demo 关于kafka安装步骤可见文章 http://www.cnblogs.com/cnmeng ...
- selenium启动chrome模拟器模拟手机
一.如果chrome选项里边有这个模拟设备(比如iPhone 6 Plus): 1.先启动Selenium Grid, 比如命令:java -jar selenium-server-standalon ...