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 ...
随机推荐
- django-分页(非海量数据)
views.py class AnalysisDataHandler(View): def get(self, request): analysis_data = MonitorCenterDataA ...
- maven的核心概念——聚合
第十六章聚合 16.1 为什么要使用聚合 将多个工程拆分为模块后,需要手动逐个安装到仓库后依赖才能够生效.修改源码后也需要逐个手动进行clean操作.而使用了聚合之后就可以批量进行Maven工程的安装 ...
- Reg文件操作
注册表REG脚本文件测试 1.新建主键 例如,想在主键[HKEY_CURRENT_USER\Software]下新建一个名叫“新建主键名称”的主键. 可以打开记事本,写入如下内容: Windows R ...
- Math, Date,JSON对象
Math 对象 Math是 JavaScript 的原生对象,提供各种数学功能.该对象不是构造函数,不能生成实例,所有的属性和方法都必须在Math对象上调用. 静态属性 Math对象的静态属性,提供以 ...
- windows定时重启
先准备好脚本restart.bat 新建一个txt,写入shutdown shutdown -s -t 10 十秒后重启,更改后缀为.bat批处理文件,切记编辑好后缀千万不要直接点开,否则就会直接调用 ...
- 关于文件中"wb"与"rb"的理解
“rb”,”wb”这两种方式在操作文件时,直接跳过了系统的编码方式,在windows系统中,用的编码为gbk: ①:with open(“a.txt”,”w”) as f1: F1.write(“aa ...
- JavaDay9(上)
Java learning_Day9(上) 本人学习视频用的是马士兵的,也在这里献上 <链接:https://pan.baidu.com/s/1qKNGJNh0GgvlJnitTJGqgA> ...
- 问题 D: 八皇后
#include <cstdio> #include <vector> #include <algorithm> using namespace std; cons ...
- 02-SV数据类型
1.数据类型 内建数据类型:逻辑(logic)类型.双状态数据类型(bit,byte,shortint,int,longint).四状态数据类型(integer,time,real) 其他:定宽数组. ...
- BZOJ2190 SDOI2008 仪仗队 gcd,欧拉函数
题意:求从左下角能看到的元素个数 引理:对点(x,y),连线(0,0)-(x,y),元素个数为gcd(x,y)-1(中间元素) 即要求gcd(x,y)=1 求gcd(x,y)=1的个数 转化为2 \s ...