Codeforces Round #621 (Div. 1 + Div. 2)D(最短路,图)
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
vector<int>v[];
int vis[];
int dis[];
int vis2[];
int dis2[];
void dijkstra(int x){
dis[x]=;
priority_queue<pair<int,int> >pq;
pq.push({,x});
while(!pq.empty()){
int now=pq.top().second;
pq.pop();
if(vis[now])
continue;
vis[now]=;
for(int i=;i<v[now].size();++i){
int t=v[now][i];
if(vis[t])
continue;
if(dis[now]+<dis[t]){
dis[t]=dis[now]+;
pq.push({-dis[t],t});
}
}
}
}
void dijkstra2(int x){
dis2[x]=;
priority_queue<pair<int,int> >pq;
pq.push({,x});
while(!pq.empty()){
int now=pq.top().second;
pq.pop();
if(vis2[now])
continue;
vis2[now]=;
for(int i=;i<v[now].size();++i){
int t=v[now][i];
if(vis2[t])
continue;
if(dis2[now]+<dis2[t]){
dis2[t]=dis2[now]+;
pq.push({-dis2[t],t});
}
}
}
}
pair<int,int>b[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m,k;
cin>>n>>m>>k;
for(int i=;i<=k;++i)
cin>>a[i];
for(int i=;i<=m;++i){
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=;i<=n;++i){
dis[i]=1e9;
dis2[i]=1e9;
}
dijkstra();
dijkstra2(n);
int ans=;
//在两个点x和y之间连边,如果经过xy这条边的路径成为新的最短路,这条路的长度为min(dis[x]+dis2[y]+1,dis[y]+dis2[x]+1)
//移项可得当dis[x]-dis2[x]<=dis[y]-dis2[y]时,这条路长度为dis[x]+dis2[y]+1,所以以dis[x]-dis2[x]大小排序,排在数组前面的点取和1的距离,后面枚举和n的距离
for(int i=;i<=k;++i)
b[i]=make_pair(dis[a[i]]-dis2[a[i]],a[i]);//b数组中的点取和1的距离
sort(b+,b++k);
int temp=dis[b[].second];
for(int i=;i<=k;++i){
ans=max(ans,temp+dis2[b[i].second]+);//当前点取和n的距离
temp=max(temp,dis[b[i].second]);
}
ans=min(ans,dis[n]);//和最短路作比较,如果最短路更短,那么将不会走其他路
cout<<ans<<"\n";
return ;
}
Codeforces Round #621 (Div. 1 + Div. 2)D(最短路,图)的更多相关文章
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- nodejs编程优化
如何编写优化的 JavaScript 对象属性的顺序:始终以相同的顺序实例化对象属性,以便共享的隐藏类和随后优化的代码可以共享之. 动态属性:在实例化之后向对象添加属性将强制执行隐藏的类更改,并降 ...
- C# monitor keyboard and mouse actions based on MouseKeyHook.
1.Install-package MouseKeyHook 2. using Gma.System.MouseKeyHook; using System; namespace ConsoleApp1 ...
- H5网页布局+css代码美化
HTML5的结构化标签,对搜索引擎更友好 li 标签对不利于搜索引擎的收录,尽量少用 banner图片一般拥有版权,不需要搜索引擎收录,因此可以使用ul + li <samp></s ...
- [大数据技术]Kettle从CSV文件读取清洗后到MySQL中文乱码问题
首先要知道CSV文件的编码格式 然后在文件输入编码选择编码格式, 第二步,在每个转换或者作业的DB连接中选择选项,并添加如下内容: 中文乱码问题得到解决
- script标签的async和defer
兼容性 IE对于defer一直都支持,async属性IE6-9都没有支持,IE10及以上支持 相同点与不同点 带有async或defer的script都会立刻下载并不阻塞页面解析,而且都提供一个可选的 ...
- matplotlib调整子图大小
因为子图太多而导致每个子图很小,很密,如何调整
- 关于MySQL数据库中null的那些事
在mysql数据库中,null是一个经常出现的情况,关于mysql中的null,有哪些注意事项呢?下面简单总结归纳下,后续会不断补充. 1. is null 首先判断数据库中某一列的值是否为null, ...
- 数据库中的sql语句总结
初识SQL 1. 什么是SQL:结构化查询语言(Structured Query Language). 2. SQL的作用:客户端使用SQL来操作服务器. > 启动mysql.exe,连 ...
- Nuxt服务端使用Axios调用接口时传递cookies
个人博客 地址:http://www.wenhaofan.com/article/20190321183709 介绍 在做单点登录时,后端需要根据cookie获取登录用户,由于前端项目使用了Nuxt做 ...
- Spring里面AOP的和IOC的理解
1.Spring里面AOP的理解:https://www.cnblogs.com/hongwz/p/5764917.html例如日志功能.日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心 ...