题意:

给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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. 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 ...

  6. 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 ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. 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 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. Dijkstra算法依据项目改进版,输出路径

    package dijkstra; import java.util.ArrayList; public class Dijkstra { ; /*private static int[][] Gra ...

  2. Codeforce 519B - A and B and Compilation Errors

    A and B are preparing themselves for programming contests. B loves to debug his code. But before he ...

  3. Proxy SwitchyOmega 使用黑名单和白名单

    “黑名单”会告诉代理工具,黑名单(国外)里面的网站要使用代理:“白名单”会告诉代理工具,白名单(大陆网站)里面的网站直接连接,其余使用代理. 黑名单PAC 黑名单PAC两条(任选其一):https:/ ...

  4. Web基础-Uri跟Url的区别

    关于URL和URI的区别,个人见解.    初学java,最近被一个概念搞得头晕脑胀,就是url和uri的概念和区别,网上查了一大通,发现各种回答眼花缭乱,有百科直接粘贴的,有胡说八道的,有故意绕来绕 ...

  5. PP: Neural ordinary differential equations

    Instead of specifying a discrete sequence of hidden layers, we parameterize the derivative of the hi ...

  6. 将字符串转换为double类型的list

    var data=“3.039,3.977,3.677,5.855,12.341,6.771”; 方法一: var result=datas.Split(',').ToList().ConvertAl ...

  7. 一文带你看清HTTP所有概念(转)

    一文带你看清HTTP所有概念   上一篇文章我们大致讲解了一下 HTTP 的基本特征和使用,大家反响很不错,那么本篇文章我们就来深究一下 HTTP 的特性.我们接着上篇文章没有说完的 HTTP 标头继 ...

  8. 【Unity|C#】基础篇(10)——泛型(Generic)/ 泛型约束条件(where)

    [学习资料] <C#图解教程>(第17章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...

  9. date-fns时间库的基本使用

    在react中使用date-fns: import sub_days from 'date-fns/sub_days'; import start_of_week from 'date-fns/sta ...

  10. Windows常用批处理命令 CMD BAT (CMD目录操作等dir)

    Windows常用批处理 CMD BAT CMD获取文件夹下所有文件的名称并打印(输出)到指定目录 dir C:\Users\\Desktop\png\*.*/s >c:\.txt //cmd ...