C. The Two Routes

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/602/problem/C

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 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 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 ≤ nu ≠ 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

4 2
1 3
3 4

Sample Output

2

HINT

题意

给你一个完全图,里面的边不是火车道就是汽车道,然后任意时刻,火车和汽车都不能相遇在除了1,n的其他点

每条边的边权值都是1,然后问你最小时间使得两种车都能到达n点

题解:

因为是完全图,那么总有一种车可以只花费1就能从起点走到终点

然后剩下那个车跑一发最短路就好了~

代码:

#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std; int mp1[][];
int mp2[][];
const int inf = 1e7+;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i==j)mp1[i][j]=mp2[i][j]=;
else mp1[i][j]=mp2[i][j]=inf;
for(int i=;i<=m;i++)
{
int x,y;scanf("%d%d",&x,&y);
mp1[x][y]=mp1[y][x]=;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i!=j)
{
if(mp1[i][j]==)mp2[i][j]=inf;
else mp2[i][j]=;
}
int flag = mp1[][n];
if(flag == inf)
{
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
mp1[i][j]=min(mp1[i][j],mp1[i][k]+mp1[k][j]);
if(mp1[][n]==inf)return puts("-1");
else printf("%d\n",mp1[][n]);
}
else
{
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
mp2[i][j]=min(mp2[i][j],mp2[i][k]+mp2[k][j]);
if(mp2[][n]==inf)return puts("-1");
else printf("%d\n",mp2[][n]);
} }

Codeforces Round #333 (Div. 2) C. The Two Routes flyod的更多相关文章

  1. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  2. Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分

    B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...

  3. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  4. Codeforces Round #333 (Div. 2) A. Two Bases 水题

    A. Two Bases Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/problem/ ...

  5. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  6. Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并

    D. Acyclic Organic Compounds   You are given a tree T with n vertices (numbered 1 through n) and a l ...

  7. Codeforces Round #333 (Div. 2)

    水 A - Two Bases 水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了.被hack掉了.用long long能存的下 #include < ...

  8. Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈

    题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...

  9. Codeforces Round #333 (Div. 2) B

    B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...

随机推荐

  1. Android:真机调试,不显示logcat的解决方案

    有时做开发的时候,用真机测试,总是看不到logcat信息 .原因是系统默认关闭了log,需要将其打开. 解决办法如下:   在拨号界面输入*#*#2846579#*#* ,然后系统会自动弹出一个菜单, ...

  2. wifi详解(一)

    1        WLAN技术 WLAN是英文WirelessLAN的缩写,就是无线局域网的意思.无线以太网技术是一种基于无线传输的局域网技术,与有线网络技术相比,具有灵活.建网迅速.个人化等特点.将 ...

  3. [Everyday Mathematics]20150106

    (1). 设 $f\in C[0,T]$, $g$ 是 $T$-周期函数, 试证: $$\bex \vlm{n}\int_0^T f(x)g(nx)\rd x=\frac{1}{T}\int_0^T ...

  4. 1、ListView自定义控件下拉刷新(一)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layo ...

  5. asp.net MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别

    @Html.Action:需要有对应的Action,并且Action方法有返回值.(注:处理完业务逻辑同时,也需要返回所需值) @{Html.RenderAction}:需要有对应的Action,Ac ...

  6. Ubuntu 12.04 pppoe拨号问题

    我的系统信息: Ubuntu 12.04.4 X64 Q001: 我学校需要使用pppoe拨号上网.我在宿舍架了个路由,可以使用无线连接拨号上网,也可以使用网线连接.在ubuntu下,使用无线连接时没 ...

  7. 三、python高级特性(切片、迭代、列表生成器、生成器)

    1.python高级特性 1.1切片 list列表 L=['Mli','add','sal','saoo','Lkkl'] L[0:3]  #即为['Mli','add','sal']  从索引0开始 ...

  8. 【Hadoop代码笔记】Hadoop作业提交之JobTracker接收作业提交

    一.概要描述 在上一篇博文中主要描述了JobTracker接收作业的几个服务(或功能)模块的初始化过程.本节将介绍这些服务(或功能)是如何接收到提交的job.本来作业的初始化也可以在本节内描述,但是涉 ...

  9. 生成chm文档工具- Sandcastle -摘自网络

    Sandcastle是微软官方的文档生成工具,NDoc开发停止后,这个貌似也是唯一的一个这方面的工具.它从dll文件及其xml注释文件能够 生成完整的帮助文档,支持多种生成格式(Helpe1x:chm ...

  10. 修改 jquery easyui 表单验证默认的样式

    目前对于不符合要求的输入域会在右侧显示一个带箭头的提示,可是如果我的输入框比较靠右的话就显示不全了(虽然会出滚动条,但是由于鼠标移开就消失了,所以还是看不到提示内容)! 能不能把这个提示的位置改变一下 ...