Codeforces Round #333 (Div. 1)
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.
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*(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)的更多相关文章
- 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 ...
- Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分
B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...
- 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 ...
- 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 ...
- 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/ ...
- 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 ...
- 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 ...
- Codeforces Round #333 (Div. 2)
水 A - Two Bases 水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了.被hack掉了.用long long能存的下 #include < ...
- Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈
题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...
- Codeforces Round #333 (Div. 2) B
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
随机推荐
- html自定义垂直导航菜单
html自定义垂直导航菜单(目前只支持上级+下级两级菜单) 由于工作的需要,昨天花了三个多小时的事件整理了一份关于垂直导航二级菜单,可以通过js配置的方式初始化菜单box(测试环境:chrome 49 ...
- log4j 2整理
# Log4j 2最佳实践 #Log4j的1.x版本已经被广泛使用于很多应用程序中.然而,它这些年的发展已经放缓.它变得越来越难以维护,因为它需要严格遵循很老的Java版本,并在2015年8月寿终正寝 ...
- pat1011. World Cup Betting (20)
1011. World Cup Betting (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Wit ...
- 《腾讯游戏人生》微信小程序开发总结
为打通游戏人生擂台赛与线下商家的O2O衔接,同时响应时下日臻火热的微信小程序,项目团队决定也开发一款针对性的微信小程序,以此方便商家在我们平台入驻并进行擂台赛事的创建和奖励的核销,进一步推广擂台赛的玩 ...
- Java学习第十九天
1:异常(理解) (1)程序出现的不正常的情况. (2)异常的体系 Throwable |--Error 严重问题,我们不处理. |--Exception |--RuntimeException 运行 ...
- solidity数据类型
1.Bool类型 取值:true/false 运算符:! && || == != 2.Integer整型 uint8-uint256 int8-int256 uint == uint ...
- VS2008、 VS2010 、 VS2012、 VS2013 都能用的快捷键
VS2008. VS2010 . VS2012. VS2013 都能用的快捷键 Ctrl+E,D --格式化全部代码 Ctrl+K,F --格式 ...
- hello2详解
1.GreetingServlet.java(显示问候页面表单) 此servlet重写该doGet方法,实现GETHTTP方法.servlet显示一个简单的HTML问候表单,其提交按钮就像hello1 ...
- Linux 套接字编程 - TCP连接基础
第五章的内容,实现一个echo服务器和对应的客户端,主要收获: 0. TCP socket编程主要基本步骤 1. SIGCHLD信号含义(子进程退出时向父进程发送,提醒父进程对其状态信息进行一个获取) ...
- scss-声明变量与引用
一.变量的声明 使用$符号可以标识一个变量 $bg-color: #FFFFFF; 二.变量的引用: 变量的引用有一个原则,那就是标准css属性值存在的地方,变量就可以存在. 当编译成css文件的时候 ...