Codeforces Round #621 (Div. 1 + Div. 2) D
题意:
给n,m,k,有n个点,m条线,距离都是一;
有k个特殊点,选择其中两个,进行相连,距离变为1,使得原本的最短路,经过相连改变小或者不变,最终结果是所有结果里面的最大距离。
思路:
选择i,j两个点(满足从1出发先遇到 i ,在遇到 j ),1~i+j~n+1就是新的最短路(1~i表示 1 到 i 最短距离,j~n表示 j 到 n 最短距离)
所以只要算出1到所有点的最短距离dis[0][N] 和 n到所有点的最短距离dis[1][N],再选择特殊点进行相连,因为最大化的话,肯定是要选择两个特殊点靠得近的排序
同时,满足特殊点按照从1出发先遇到的顺序来,因为可能是先遇到 j 点,导致 会有1~i+j~n+1>1~j+i~n+1的情况
比较就用1~i+j~n<1~j+i~n,然后操作有点像尺缩,叙述能力有限
最后还要进行与1~n进行比较大小,这个用图解释比较直观

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define il inline
#define it register int
#define inf 0x3f3f3f3f
#define lowbit(x) (x)&(-x)
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 998244353
const int N=2e5+;
struct node{
int v,next;
}d[N<<];
struct node1{
int x,bu;
node1(){}
node1(int xx,int buu):x(xx),bu(buu){}
friend bool operator<(const node1 a,const node1 b){
if(a.bu==b.bu){
return a.x>b.x;
}
return a.bu>b.bu;
}
};
int n,m,k;
int a[N],head[N],tot;
int dis[][N];
il void add(int u,int v){
d[tot].v=v;d[tot].next=head[u];
head[u]=tot++;
}
void bfs(int x,int c){
dis[c][x]=;
priority_queue<node1>q;
q.push(node1(x,));
while(!q.empty()){
node1 t=q.top();q.pop();
int u=t.x,bu=t.bu;
for(it i=head[u];~i;i=d[i].next){
int v=d[i].v;
if(dis[c][v]==-){
dis[c][v]=bu+;
q.push(node1(v,bu+));
}
}
}
}
bool cmp(int x,int y){
return dis[][x]+dis[][y]<dis[][y]+dis[][x];
}
int main(){
tot=;
scanf("%d%d%d",&n,&m,&k);
for(it i=;i<=n;i++){head[i]=-,dis[][i]=dis[][i]=-;}
for(it i=;i<k;i++){
scanf("%d",&a[i]);
}
for(it i=;i<m;i++){
int u,v;scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
bfs(,);bfs(n,);
sort(a,a+k,cmp);
int ans=dis[][a[]],da=-;
for(it i=;i<k;i++){
da=max(da,ans+dis[][a[i]]+);
ans=max(ans,dis[][a[i]]);
}
printf("%d\n",min(dis[][n],da));
return ;
}
/*
5 5 2
2 4
1 5
4 5
3 4
2 3
1 2
*/
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 ...
随机推荐
- hive删除表时直接卡死
原因:因为以前安装的mysql,字符集都改为了utf-8. 解决方案:需要把字符集都改为latin1 首先进入mysql查看字符集 show variables like 'char%' 找到mysq ...
- AntDesign(React)学习-1 创建环境
目录: AntDesign(React)学习-15 组件定义.connect.interface AntDesign(React)学习-14 使用UMI提供的antd模板 AntDesign(Reac ...
- nginx配置https域名
nginx安装配置支持https和配置https域名 yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-de ...
- vs2017+resharper之常用快捷键备忘
1.安装resharper后以vs2017的快捷键为主,让resharper作为一些方便的快捷键的补充. 2.vs2017的c++6的键盘布局模式快捷键 IntelliSence: 列表成员: Ctr ...
- app点击底部菜单切换标题
<!DOCTYPE HTML><html><head> <meta charset="utf-8"> <meta name=& ...
- 马走日的解法(dfs)
马在中国象棋以日字形规则移动. 请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点. Input 第一行为整数T ...
- 题解【洛谷P1618】 三连击(升级版)
设三个数分别为n1.n2.n3,因为三个数的比为A:B:C,取一份量i,使得A·i=x,B·i=y,C·i=z(·是*的意思). 所以我们的代码只需要枚举i,并以此判断n1.n2.n3是否为三位数且包 ...
- Visibility Graph Analysis of Geophysical Time Series: Potentials and Possible Pitfalls
Tasks: invest papers 3 篇. 研究主动权在我手里. I have to. 1. the benefit of complex network: complex networ ...
- 显示当前目录命令 - pwd
1) 命令名称:pwd 2) 英文原意:print working directory 3) 命令所在路径:/bin/pwd 4) 执行权限:所有用户 5) 功能描述:显示当前目录 6) 语法: pw ...
- 解决ERROR 1130: Host 'x.x.x.x' is not allowed to connect to this MariaDB server 方法
问题描述 在使用SQLyog操作Linux上的MariaDB时候,会出现如下错误: 解决方法 改表法 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电 ...