Caravans

Time limit: 1.0 second
Memory limit: 64 MB
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 ≤ 105; 2 ≤ m ≤ 105), 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.

Sample

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.
Problem Author: Oleg Merkurev
【分析】给你一个图,起点和终点还有强盗所在的点,商家运货只走最短路,强盗想劫持这批货,但强盗很懒,只想埋伏在一个离他所在点最近的一个点且那个点在商家的路线上。问强盗最远需要走多少距离,也就是说商家选择的最短路是不定的,也许选择了一条离强盗家最远的那条路,但强盗仍然要埋伏在这条路上离他家最近的那个点上。
做法就是两次SPFA。第一次用dis数组记录每个点到强盗家的最短路,第二次维护两个数组,d[]记录每个点到s的最短路,ans[]记录强盗需要走的最远的距离。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 1e5+;
const int M = ;
int n,m,k,tot=,s,t,r;
int dis[N],head[N],vis[N],ans[N],d[N];
queue<int>q;
struct man{
int to,next;
}edg[*N];
void add(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
edg[tot].to=u;edg[tot].next=head[v];head[v]=tot++;
}
void spfa1(int x){
met(vis,);met(d,inf);
vis[x]=;ans[x]=dis[x];d[x]=;
while(!q.empty())q.pop();
q.push(x);
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(d[v]>d[u]+){
d[v]=d[u]+;
ans[v]=min(ans[u],dis[v]);
if(!vis[v]){
q.push(v);vis[v]=;
}
}
else if(d[v]==d[u]+ && ans[v]<ans[u]){
ans[v]=min(ans[u], dis[v]);
if(!vis[v]){
q.push(v);vis[v]=;
}
}
}
}
}
void spfa(int x){
met(vis,);met(dis,inf);
vis[x]=;dis[x]=;
while(!q.empty())q.pop();
q.push(x);
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(dis[v]>dis[u]+){
dis[v]=dis[u]+;
if(!vis[v]){
q.push(v);vis[v]=;
}
}
}
}
}
int main() {
scanf("%d%d",&n,&m);
int u,v;met(head,-);
while(m--){
scanf("%d%d",&u,&v);
add(u,v);
}
scanf("%d%d%d",&s,&t,&r);
spfa(r);
spfa1(s);
printf("%d\n",ans[t]);
return ;
}

URAL 2034 Caravans(变态最短路)的更多相关文章

  1. URAL 2034 : Caravans

    Description   Student Ilya often skips his classes at the university. His friends criticize him for ...

  2. URAL 1085 Meeting(最短路)

    Meeting Time limit: 2.0 secondMemory limit: 64 MB K friends has decided to meet in order to celebrat ...

  3. URAL 1934 Black Spot(最短路)

    Black Spot Time limit: 1.0 secondMemory limit: 64 MB Bootstrap: Jones's terrible leviathan will find ...

  4. URAL 2069 Hard Rock (最短路)

    题意:给定 n + m 个街道,问你从左上角走到右下角的所有路的权值最小的中的最大的. 析:我们只要考虑几种情况就好了,先走行再走列和先走列再走行差不多.要么是先横着,再竖着,要么是先横再竖再横,要么 ...

  5. URAL

    URAL 2035 输入x,y,c,  找到任意一对a,b 使得a+b==c&& 0<=a<=x && 0<=b<=y 注意后两个条件,顺序搞错 ...

  6. Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)

    1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...

  7. DP/最短路 URAL 1741 Communication Fiend

    题目传送门 /* 题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版 正版+破解版->正版,盗版+破解版->盗版 DP:每种情况考虑一 ...

  8. URAL 1002 Phone Numbers(KMP+最短路orDP)

    In the present world you frequently meet a lot of call numbers and they are going to be longer and l ...

  9. URAL 1056 Computer Net(最短路)

    Computer Net Time limit: 2.0 secondMemory limit: 64 MB Background Computer net is created by consecu ...

随机推荐

  1. Excel公式中双引号和单引号输入和显示以及函数的选择确认

    [Excel中显示双引号] 1.直接输入双引号“”或单引号“ 2.工式中显示双引号需输入“”“”“”(六个引号)或单引号需输入“”“”(四个引号) [Excel中快速确认已选择的函数] 1.用键盘的上 ...

  2. C# 数据流操作 Stream 相关

    FileStream:對文件執行讀取與寫入 MemoryStream:對內存進行讀取與寫入 BufferedStream:對緩沖器進行讀取與寫入 StreamReader/StreamWriter 命 ...

  3. 教学目标的表述方式──行为目标的ABCD表述法

    教学目标应规定学生在教学活动结束后能表现出什么样的学业行为,并限定学生学习过程中知识.技能的获得和情感态度发展的层次.范围.方式及变化效果的量度.对每节课教学目标的准确表述,可以充分发挥教学目标在教学 ...

  4. [转]Android系统Surface机制的SurfaceFlinger服务简要介绍和学习计划

    转自:Android系统Surface机制的SurfaceFlinger服务简要介绍和学习计划 前面我们从Android应用程序与SurfaceFlinger服务的关系出发,从侧面简单学习了Surfa ...

  5. .net 找回密码的第一步 第二步 第三步的进程条

    先写一个div作为存放这个进程条的容器 开始写js 根据jQuery选择器找到需要加载ystep1的容器 loadstep方法可以初始化 steps参数表示步骤名称,content则是鼠标移动到当前位 ...

  6. IIS使用问题

    1.System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序:修改IIS应用程序池的高级设置将32位设置成true

  7. Linksys WRT120N路由器备份文件解析

    Perusing the release notes for the latest Linksys WRT120N firmware, one of the more interesting comm ...

  8. Multiple dex files define

    Multiple dex files define 在项目中,有一个类的包名和引用的jar包中的类和包名一致

  9. 让一个小Div(子)在大Div(父)中垂直水平都居中

    方法1: .parent {          width:800px;          height:500px;          border:2px solid #000;          ...

  10. 正确的SVN导入代码命令

    sudo svn import -m "" /media/yang/10/ubuntuFiles/svnAbout/mypro/ svn://127.0.0.1/mypro/