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. Python学习 day07

    一.关于解决问题的思路 1.删除列表中索引为单数的元素. 别人的思路: 利用切片 li = [11, 22, 33, 44, 55] li = li[::2] print(li) 思考:虽然学了pyt ...

  2. cloudermanger安装时需要安装或彻底正确卸载再安装orcal-java7-installer、oracle-java7-set-default(ubuntu14.04版本)(图文详解)

    不多说,直接上干货! 安装orcal-java7-installer和oracle-java7-set-default 安装JDK1.7 (所有节点)CDH要求至少是Oracle JDK7,Ubunt ...

  3. java多线程之join方法使用

    看这篇博客:http://www.cnblogs.com/skywang12345/p/3479275.html

  4. 用PHP实现同一个帐号不允许同时登陆,只允许一个帐号登录?

    数据库表 user_login_info 字段:id,user_ip,user_id,last_access_time user_id 做唯一性索引 1. 用户登录后 如果没有当前用户的数据,插入一条 ...

  5. nagios监控远程端口

    check_port 位置:/usr/local/nagios/libexec/ 代码(新建可执行文件) #!/bin/sh /usr/local/nagios/libexec/check_tcp - ...

  6. 白话SpringCloud | 第十一章:路由网关(Zuul):利用swagger2聚合API文档

    前言 通过之前的两篇文章,可以简单的搭建一个路由网关了.而我们知道,现在都奉行前后端分离开发,前后端开发的沟通成本就增加了,所以一般上我们都是通过swagger进行api文档生成的.现在由于使用了统一 ...

  7. nginx安装及优化

    1.pcre及nginx安装包下载 wget http://www.pcre.org/   pcre用yum安装即可 http://nginx.org/en/download.html 2.安装 -安 ...

  8. JavaScript字符串去除空格

    /*字符串去除空格*/ String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, "") ...

  9. C# 判断List集合中是否有重复的项

    /*在.Net 3.5 以上*/ ).Count() >= ;

  10. scss-@if指令

    @if指令接受表达式和使用嵌套样式,无论表达式的结果只不过是false或null. 语法: @if expression { //CSS codes are written here } scss实例 ...