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 ...
随机推荐
- winform DataGrid排序、去掉第一的空白列
排序: dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Descending); 去掉空白列: dataGridView1 ...
- Selenium3+python自动化009- js之屏幕滑动和日历操作
一.js的滑屏 1)以下脚本实现js滑屏scroll="document.documentElement.scrollTop=800"#垂直滚动 pxscroll = " ...
- asm相关内容想下载(包括 jar 包)
网址:http://download.forge.ow2.org/asm/
- ubuntu18.04 编译fortran出现 ‘没有f951这个文件’处理
机器自带了gcc所以可以编译fortran文件, 使用时, gcc **.for –o ***.out 提示,没有找到f951. 然后去网上找解决方案,有的人说在其他地方找到了f951,然后把他复制到 ...
- 登录时 按Enter 进入登录界面 或者下一行
function keyLogin() { if (event.keyCode == 13) //回车键的键值为13 $(".btn-submit").click(); //调用登 ...
- 判断IE版本的HTML语句[if lte IE 9]……[endif]
我们常常会在网页的HTML里面看到形如[if lte IE 9]……[endif]的代码,表示的是限定某些浏览器版本才能执行的语句,那么这些判断语句的规则是什么呢?请看下文: <!--[if ! ...
- bugku sql2
sql注入2 200 http://123.206.87.240:8007/web2/ 全都tm过滤了绝望吗? 提示 !,!=,=,+,-,^,%
- NVMe over Fabrics 协议Discovery服务交互过程跟踪
Discovery服务过程跟踪 对于NVMe over Fabrics的subsystem,有两种类型:Discovery子系统和NVM子系统.这里介绍与Discovery子系统相关的交互内容(即:在 ...
- Myeclipse异常
打不开文件 问题描述:Myeclipse然打开什么东西都报错了:Could not open the editor: Invalid thread access 解决方法:1.cmd 2.cd 进入你 ...
- Vue中父组件向子组件echarts传值问题
原文链接:https://blog.csdn.net/Uookic/article/details/80638883?utm_source=copy 问题:当父组件传值给子组件echarts时,发现子 ...