Codeforces Round #621 (Div. 1 + Div. 2) D

Meaning of the questions:

For n, m, K, there are n points, m lines, are a distance;

There are special points k, where two select, for connected distance becomes 1, so that the original shortest, is connected via a small change or change, the end result is the result of all the maximum distance inside.

Ideas:

Select i, j two points (one starting from the first face to meet i, in the face j), 1 ~ i + j ~ n + 1 is the new shortest path (1 ~ i 1 to i represents the shortest distance, j ~ n n represents the shortest distance to j)

So long as the calculated 1 to all points of the shortest distance dis [0] [N] and n shortest distance dis all points [1] [N], and then select a particular point is connected, because maximized, is certainly to select two special points are close sort

Meanwhile, in order to meet the specific point from 1 to encounter, as may be encountered to point j, there will lead to 1 ~ i + j ~ n + 1> 1 ~ j + i ~ n + 1 is

Comparison to use 1 ~ i + j ~ n <1 ~ j + i ~ n, then operates somewhat like length contraction, described limited capacity

Finally, also for comparing the size of 1 ~ n, this visual comparison with FIG explained

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define il inline
 5 #define it register int
 6 #define inf 0x3f3f3f3f
 7 #define lowbit(x) (x)&(-x)
 8 #define mem(a,b) memset(a,b,sizeof(a))
 9 #define mod 998244353
10 const int N=2e5+10;
11 struct node{
12     int v,next;
13 }d[N<<1];
14 struct node1{
15     int x,bu;
16     node1(){}
17     node1(int xx,int buu):x(xx),bu(buu){}
18     friend bool operator<(const node1 a,const node1 b){
19         if(a.bu==b.bu){
20             return a.x>b.x;
21         }
22         return a.bu>b.bu;
23     }
24 };
25 int n,m,k;
26 int a[N],head[N],tot;
27 int dis[2][N];
28 il void add(int u,int v){
29     d[tot].v=v;d[tot].next=head[u];
30     head[u]=tot++;
31 }
32 void bfs(int x,int c){
33     dis[c][x]=0;
34     priority_queue<node1>q;
35     q.push(node1(x,0));
36     while(!q.empty()){
37         node1 t=q.top();q.pop();
38         int u=t.x,bu=t.bu;
39         for(it i=head[u];~i;i=d[i].next){
40             int v=d[i].v;
41             if(dis[c][v]==-1){
42                 dis[c][v]=bu+1;
43                 q.push(node1(v,bu+1));
44             }
45         }
46     }
47 }
48 bool cmp(int x,int y){
49     return dis[0][x]+dis[1][y]<dis[0][y]+dis[1][x];
50 }
51 int main(){
52     tot=0;
53     scanf("%d%d%d",&n,&m,&k);
54     for(it i=0;i<=n;i++){head[i]=-1,dis[1][i]=dis[0][i]=-1;}
55     for(it i=0;i<k;i++){
56         scanf("%d",&a[i]);
57     }
58     for(it i=0;i<m;i++){
59         int u,v;scanf("%d%d",&u,&v);
60         add(u,v),add(v,u);
61     }
62     bfs(1,0);bfs(n,1);
63     sort(a,a+k,cmp);
64     int ans=dis[0][a[0]],da=-1;
65     for(it i=1;i<k;i++){
66         da=max(da,ans+dis[1][a[i]]+1);
67         ans=max(ans,dis[0][a[i]]);
68     }
69     printf("%d\n",min(dis[0][n],da));
70     return 0;
71 }
72 /*
73 5 5 2
74 2 4
75 1 5
76 4 5
77 3 4
78 2 3
79 1 2
80 */
View Code

 

Guess you like

Origin www.cnblogs.com/luoyugongxi/p/12327170.html