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 ...
随机推荐
- Electron – 项目报错整理(打包~2): electron-packager踩坑
- K8S 概述
K8S------概述 K8S,就是基于容器的集群管理平台,它的全称,是kubernetes.Kubernetes 这个单词来自于希腊语,含义是舵手或领航员.K8S是它的缩写,用“8”字替代了“ube ...
- 分享10个免费或便宜的Photoshop替代工具
说到编辑照片和图像文件,一般很多人都使用photoshop软件.然而,使用现在的最新版本Photoshop CC每月最低也要支付980日元,感觉使用门槛有点高的人应该不少吧. 有一篇文章,推荐了10个 ...
- phpcmsv9 后台统计编辑发稿数量
直切正题: 每个人,每个栏目,发稿数量统计 SELECT a.realname AS 姓名, c.catname AS 栏目名称, count(1) AS 发稿量FROM v9_news bz, v9 ...
- 使用DataContractJsonSerializer发序列化对象时出现的异常
最近服务器上的某个程序的错误日志中频繁出现以下异常: Deserialising: There was an error deserializing the object of type {type} ...
- Codeforces Round #613 (Div. 2) (A-E)
A略 直接求和最大的子序列即可(注意不能全部选中整个子序列) or #include<bits/stdc++.h> using namespace std; void solve(){ i ...
- Agri-Net POJ - 1258 prim
#include<iostream> #include<cstdio> #include<cstring> using namespace std; ; #defi ...
- [ERR] Node goodsleep.vip:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决方案 以前的cluster节点信息 保留 要删除 dump.rdb node.conf集群启动时自动生成文件
- Spring Batch 批处理原则与建议
Spring Batch 批处理原则与建议 当我们构建一个批处理的过程时,必须注意以下原则: 通常情况下,批处理的过程对系统和架构的设计要够要求比较高,因此尽可能的使用通用架构来处理批量数据处理,降低 ...
- musql 单表查询
一 介绍 本节内容: 查询语法 关键字的执行优先级 简单查询 单条件查询:WHERE 分组查询:GROUP BY HAVING 查询排序:ORDER BY 限制查询的记录数:LIMIT 使用聚合函数查 ...