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 ...
随机推荐
- 转:静态链表及其创建(C语言实现)
http://c.biancheng.net/view/3339.html 折半插入排序算法(C语言代码实现) http://c.biancheng.net/view/3440.html 二叉树层次遍 ...
- import,export深入理解
export 最正常: var firstName = 'Michael'; var lastName = 'Jackson'; var year = 1958; export { firstName ...
- 剑指offer-面试题47-礼物的最大价值-动态规划
/* 题目: 给定一个m*n的棋盘,每格放一个礼物(每个礼物的值大于0), 从左上角出发,向下或向右走到达右下角,得到的礼物和最大. */ /* 思路: f(i,j)=max[f(i-1,j),f(i ...
- Fiddler: AutoResponder 构建模拟测试场景
AutoResponder 可用于拦截某一请求,并重定向到本地的资源,或者使用Fiddler的内置响应.可用于调试服务器端代码而无需修改服务器端的代码和配置,因为拦截和重定向后,实际上访问的是本地的文 ...
- Uva1639(概率期望/对数处理避免丢失精度)
Uva1639 题意: 有两个盒子各有n个糖果(n<=200000),每天随机选择一个:选第一个盒子的概率是p(0 ≤ p ≤ 1),第二个盒子的概率为1-p,然后吃掉其中的一颗.直到有一天,随 ...
- scrapy(一)--Pycharm创建scrapy项目
1.环境 操作系统:windows10. python版本:python3.6,Anaconda(将Anaconda3\Scripts;路径添加到环境变量Path中) pycharm:pycharm2 ...
- while语句的使用
学习while语句的正确用法:题目:老师教学生,讲一次之后 问学生会不会,如果不会:就再讲一遍.如果会了就放学,但是如果连讲了十遍还是不会,那也要放学回家 namespace _44讲十遍会不会 { ...
- python3-cookbook笔记:第二章 字符串和文本
python3-cookbook中每个小节以问题.解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构.函数.类等特性在某类问题上如何更好地使 ...
- HDU1163 - Eddy's digital Roots
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1163 九余数:一个数除于9所得到的余数,即模9得到的值 求九余数: 求出一个数的各位数字之和,如果是两 ...
- APFS 宗卷 • APFS(加密)磁盘格式怎么去掉?Mac磁盘加密怎么解除?
相信很多朋友都因为APFS 宗卷 • APFS(加密)磁盘格式而困扰,这种磁盘加密,导致很多破解版软件都不能安装.那么磁盘加密怎么解除?小编翻阅了一些教程,为您带来APFS 宗卷 • APFS(加密) ...