A. The Two Routes

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.

Examples
input
4 2
1 3
3 4
output
2
input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
output
-1
input
5 5
4 2
3 5
4 5
5 1
1 2
output
3
Note

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*(n-1)/2 -  m 的其余的边连上公路。而且他们走最短路是不可能相撞的。

其实样例会误导你,公路其实,可以更短1-4.

那么就是两边最短路。

#include <bits/stdc++.h>

using namespace std;

const int MAXN = ;
const int inf = 0x3f3f3f3f; struct Edge {
int from,to,dist;
}; struct HeapNode {
int d,u;
bool operator < (const HeapNode & rhs) const {
return d > rhs.d;
}
}; struct Dij {
vector<Edge> edges;
vector<int> G[MAXN];
int n,m;
bool done[MAXN];
int d[MAXN];
int p[MAXN]; void init(int n) {
this->n = n;
for(int i = ; i < n; i++) G[i].clear();
edges.clear();
} void AddEdge (int from ,int to,int dist) {
edges.push_back((Edge){from,to,dist});
m = edges.size();
G[from].push_back(m-);
} void dij(int s) {
priority_queue<HeapNode> Q;
for(int i = ; i <n; i++) d[i] = inf;
d[s] = ;
memset(done,,sizeof(done));
Q.push((HeapNode){,s});
while(!Q.empty()) {
HeapNode x = Q.top();Q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true; for(int i = ; i <(int)G[u].size(); i++) {
Edge& e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist) {
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
Q.push((HeapNode){d[e.to],e.to});
}
}
}
} }sol; bool maps[MAXN][MAXN]; int main()
{
//freopen("in.txt","r",stdin);
int n,m;
scanf("%d%d",&n,&m);
memset(maps,,sizeof(maps)); sol.init(n);
for(int i = ; i < m; i++) {
int u,v;
scanf("%d%d",&u,&v);
u--;v--;
sol.AddEdge(u,v,);
sol.AddEdge(v,u,);
maps[u][v] = maps[v][u] = ;
} sol.dij();
int ans = sol.d[n-]; sol.init(n);
for(int i = ; i < n; i++)
for(int j = i+; j < n; j++) {
if(maps[i][j]==) {
sol.AddEdge(i,j,);
sol.AddEdge(j,i,);
}
}
sol.dij();
ans = max(ans,sol.d[n-]);
if(ans==inf) cout<<-<<endl;
else cout<<ans<<endl; return ;
}

Codeforces Round #333 (Div. 1)的更多相关文章

  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) C. The Two Routes flyod

    C. The Two Routes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/pro ...

  4. 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 ...

  5. 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/ ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #333 (Div. 2)

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

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

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

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

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

随机推荐

  1. docker 部署disconf 以及将其做成镜像

    1.需要一台服务器(阿里云,腾讯云.实体服务器都行,本次是以实体服务器为依照做的) 2.安装docker   https://www.cnblogs.com/shijunjie/p/10436293. ...

  2. vm12下Centos6的javaweb环境搭建

    配置linux的javaweb环境之前: 1.在windows安装xshell(非必需,但是推荐) 2.在linux安装Linux与windows文件传输工具RZSZ[root@192 ~]# yum ...

  3. Python+Selenium之通过batch跑脚本

    例如在执行路径C:\Portal_Scripts\Scripts下的脚本CreateIndicativeBOP.py,可以在notepad里面编写如下: @echo off cd  C:\Portal ...

  4. GreenPlum 大数据平台--运维(一)

    .最后分析或真空或创建表或等... Select * from pg_stat_operations where schemaname='SCHEMA NAME ' and actionname in ...

  5. apply、call、bind区别、用法

    apply和call都是为了改变某个函数运行时的上下文而存在的(就是为了改变函数内部this的指向):   如果使用apply或call方法,那么this指向他们的第一个参数,apply的第二个参数是 ...

  6. (转)DB2 restart database命令的作用总结

    DB2 restart database命令的作用总结 原文:https://blog.csdn.net/qingsong3333/article/details/62049039 信息中心对于RES ...

  7. Beam概念学习系列之PTransform数据处理

    不多说,直接上干货! PTransform数据处理 PTransform对PCollection进行并行处理,每次处理1条,例如Filter过滤.Groupby分组.Combine统计.Join关联等 ...

  8. 遇见phpDesigner我笑了 PHP开发利器

    phpDesigner实在太好用了 相信许多PHP同胞谈到PHP开发工具的时候,都会想到Zend Studio.Eclipse等开发工具,这些工具的确非常的强大强悍,但复杂的配置满屏幕的英文并不适合所 ...

  9. tomcat-dbcp数据库连接池配置以及使用时候的一些坑

    一.数据库连接池 开发的时候经常会需要对数据库进行一些操作,比如说常见的增删改查之类的,当数据量小的时候,可以直接进行操作,但是当数据量增多的时候,每一次连接以及释放数据库都会耗费一定的时间,这个时候 ...

  10. plsql 连接 虚拟机Linux下的Oracle数据库 失败

    plsql 连接 虚拟机Linux下的Oracle数据库 失败2017年09月09日 09:10:23 守望dfdfdf 阅读数:187 标签: oraclelinux 更多个人分类: 工具 软件编辑 ...