URAL 2034 : Caravans (二分最短路)
Student Ilya often skips his classes at the university. His friends criticize him for this, but they don’t know that Ilya spends this time not watching TV serials or listening to music. He creates a computer game of his dreams. The game’s world is a forest. There are elves, wooden houses, and a villain. And one can rob caravans there! Though there is only one caravan in the game so far, Ilya has hard time trying to implement the process of robbing.
The game world can be represented as several settlements connected by roads. It is possible to get from any settlement to any other by roads (possibly, passing through other settlements on the way). The settlements are numbered by integers from 1 to n. All the roads are two-way and have the same length equal to 1. It is not allowed to move outside the roads. The caravan goes from settlement s to settlement f following one of the shortest routes. Settlement r is the villain’s den. A band of robbers from settlement r has received an assignment to rob the caravan. In the evening they will have an exact plan of the route that the caravan will take the next morning. During the night they will be able to move to any settlement on the route, even to settlement s or f. They will lay an ambush there and rob the caravan in the morning. Of course, among all such settlements the robbers will choose the one closest to settlement r. The robbers have a lot of time until the evening. They don’t know the caravan’s route yet, but they want to know the maximum distance they will have to go in the worst case to the settlement where they will rob the caravan. Help Ilya calculate this distance, and it may happen that he will attend his classes again!
Input
The first line contains integers n and m (3 ≤ n ≤ 10 5; 2 ≤ m ≤ 10 5), which are the number of settlements in the game world and the number of roads between them. Each of the following m lines describes a road. The description contains integers a and b, which are the numbers of the settlements connected by the road. It is guaranteed that each road connects two different settlements and there is at most one road between any two settlements. It is also guaranteed that the road network is connected. In the last line you are given pairwise different integers s, f, and r, which are the numbers of the settlements described above.
Output
In the only line output the required distance.
Example
input output 7 7 1 2 2 4 2 5 3 4 4 6 5 6 6 7 1 7 3 2
Notes
In the sample the caravan may follow either the route 1-2-4-6-7 or the route 1-2-5-6-7. In the first case the robbers lay an ambush in settlement 4, which is at distance 1 from the villain’s den. In the second case the robbers lay an ambush in settlement 2 or settlement 6, which are at distance 2 from the villain’s den. The second variant is worse for the robbers, and it should be chosen as the answer.
题目大意:
找一个r到最短路的最小距离的最大值;
二分答案;
先从st r开始跑一边最短路记录从st到ed得最短路res,二分答案mid,每次将小于mid的点标记,如果通过剩余的点仍能跑出res,区间向上,否则区间向下
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define For(i,a,b) for(int i=a;i<=b;i++)
const int maxn=1e5+10;
using namespace std;
struct edge{
int u,v,w,next;
}e[2*maxn];
int g[2*maxn],vis[maxn],d1[maxn],d2[maxn],ok[maxn],t[maxn],n,m,tot,res;
void creat_edge(int u,int v,int w)
{
e[++tot]=(edge){u,v,w,g[u]};
g[u]=tot;
}
void spfa(int st,int d[])
{
queue<int>q;
for(int i=1;i<=n;i++)
{
d[i]=inf;
}
mem(vis,0);
d[st]=0;
vis[st]=1;
q.push(st);
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=0;
for(int i=g[now];i>0;i=e[i].next)
{
int u=e[i].u,v=e[i].v,w=e[i].w;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
}
int spfa1(int st,int ed,int r)
{
queue<int>q;
mem(vis,0);
for(int i=1;i<=n;i++)
{
t[i]=inf;
}
t[st]=0;
vis[st]=1;
if(!ok[st]) return 1;
q.push(st);
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=0;
for(int i=g[now];i>0;i=e[i].next)
{
int u=e[i].u,v=e[i].v,w=e[i].w;
if(t[v]>t[u]+w&&ok[v])
{
t[v]=t[u]+w;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(t[ed]!=res) return 1;
return 0;
}
int cheek(int cost,int st,int ed,int r)
{
mem(ok,0);
for(int i=1;i<=n;i++)
{
ok[i] = d1[i] >= cost;
}
return spfa1(st,ed,r);
}
int main(){
int st,ed,r;
while(~scanf("%d%d",&n,&m))
{
mem(e,0);
mem(vis,0);
tot=0;
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
creat_edge(u,v,1);
creat_edge(v,u,1);
}
scanf("%d%d%d",&st,&ed,&r);
spfa(r,d1);
spfa(st,d2);
res=d2[ed];
int ll=1,rr=n;
int ans = 0;
while (ll<=rr)
{
int mid =ll+rr>>1;
if (cheek(mid,st,ed,r)) rr=mid-1;
else ans=mid,ll=mid+1;
}
cout<<ans;
}
return 0;
}