Description

 
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 sf, and r, which are the numbers of the settlements described above.

Output

In the only line output the required distance.
 
input output
7 7
1 2
2 4
2 5
3 4
4 6
5 6
6 7
1 7 3
2
 
 
 
题目大意:
大意是有一支商队要从点 s 到点 f,这个商队只会走走短路(题目保证能从 s 到 f ,同时,走每条边的时间花费都是 1),有一个强盗在点 r ,这个强盗要去抢商队
就问你在最坏情况下,强盗能够拦截到这个商队所需走的最短距离.
比如样例:
这里商队需要从点 1 到点 7 ,强盗在点 3,我们可以很容易知道商队的路线有两条:
1 -> 2 -> 4 ->6 -> 7
1 -> 2 -> 5 ->6 -> 7
而在最坏情况下,也就是商队走1->2->5->6->7时,强盗在点 3 想要拦截到商队需要至少走两步(到 2 或者 6 ) 才能拦截到商队
所以样例的输出是 2
 
 
解题报告:
首先我们肯定需要求一次最短路,这个最短路是强盗所在的点 r ,到其他各个点的最短路,我们不妨设置值为 maxv[](不要奇怪这个命名)
即maxv[ x ]表示点 r 到点 x 的最短距离.
显然我们这道题需要使用到DP(递推)求解,注意到这些递推的点必须是最短路上的点同时也必须只能转移到最短路上的点,那么我们该如何确定这个最短路的点呢?
我们令 d[] 这个数组表示从终点 f 到其他各个点的最短距离,那么我们只要在转移时必须满足 d[v] == d[u] - 1 即可(从u转移到v) <仔细想想>
这样我们就保证了递推过程的点肯定是最短路上的点,也只能转移到最短路上的点
之后我们考虑DP值,我们令DP[ i ] 表示拦截从 起点 到 i 这个点所在的最短路的最小花费
那么我们很容易的就可以得出递推方程(用 u 去更新 v)
dp[v] = max( maxv[u] } <仔细想想> ,取max是因为我们要保证最差情况
 
那么当我们扩展到点 u时,需要首先更新 maxv 值,即 maxv[u] = min{ maxv[u] , dp[u] } ,这个方程就非常容易了,因为拦截从 起点 到 点i所在的最短路不外乎就两种方式,一种在前面拦截,第二种在这个点 i 拦截,所以我们取个min即可(强盗也要走最短的对不对)
 
那么,我们就解决了这道题
 
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 1e5 + ;
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
vector<int>e[maxn];
int dp[maxn] , d[maxn] , maxv[maxn], n , m , s , f , r , used[maxn] ;
queue<int>q; void solve()
{
memset(dp , , sizeof(dp));memset(d,-,sizeof(d));memset(maxv,-,sizeof(maxv));memset(used,,sizeof(used));
d[f] = ;
q.push(f);
while(!q.empty())
{
int x = q.front();q.pop();
for(int i = ; i < e[x].size() ; ++ i)
{
int v = e[x][i];
if (d[v] == -)
{
d[v] = d[x] + ;
q.push(v);
}
}
}
maxv[r] = ;
q.push(r);
while(!q.empty())
{
int x = q.front();q.pop();
for(int i = ; i < e[x].size() ; ++ i)
{
int v = e[x][i];
if (maxv[v] == -)
{
maxv[v] = maxv[x] + ;
q.push(v);
}
}
}
dp[s] = maxv[s];used[s] = ;
q.push(s);
while(!q.empty())
{
int x = q.front();q.pop();
maxv[x] = min(maxv[x],dp[x]);
for(int i = ; i < e[x].size() ; ++ i)
{
int v = e[x][i];
if (d[x] - d[v] == )
{
dp[v] = max(dp[v],maxv[x]);
if (!used[v])
{
q.push(v);
used[v] = ;
}
}
}
}
printf("%d\n",maxv[f]);
} int main(int argc,char *argv[])
{
scanf("%d%d",&n,&m);
while(m--)
{
int u ,v ;
scanf("%d%d",&u,&v);u--,v--;
e[u].push_back(v);e[v].push_back(u);
}
scanf("%d%d%d",&s,&f,&r);s--,f--,r--;
solve();
return ;
}
 

URAL 2034 : Caravans的更多相关文章

  1. URAL 2034 Caravans(变态最短路)

    Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...

  2. URAL

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

  3. hdu 2034人见人爱A-B

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2034 解题思路:set的基本用法 #include<iostream> #include& ...

  4. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  5. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  6. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  7. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  8. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  9. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

随机推荐

  1. 简单使用NSURLConnection、NSURLRequest和NSURL

    以下是代码,凝视也写得比較清楚: 头文件须要实现协议NSURLConnectionDelegate和NSURLConnectionDataDelegate // // HttpDemo.h // My ...

  2. Vitamio 多媒体框架 介绍

    功能 Vitamio 是一款 Android 与 iOS 平台上的全能多媒体开发框架,全面支持硬件解码与 GPU 渲染.Vitamio 凭借其简洁易用的 API 接口赢得了全球众多开发者的青睐.到目前 ...

  3. nyoj 2

    #include <iostream> #include <stack> #include <string.h> #include <stdio.h> ...

  4. C# 导出word文档及批量导出word文档(3)

    在初始化WordHelper时,要获取模板的相对路径.获取文档的相对路径多个地方要用到,比如批量导出时要先保存文件到指定路径下,再压缩打包下载,所以专门写了个关于获取文档的相对路径的类. #regio ...

  5. oracle单行函数之类型转换

    oracle数据类型转换:显示转换盒隐式转换 oracle自动完成转换

  6. 使用Scanner来解析文件

    前面的流是全部流进来再处理,空间换取时间 我们用Scanner来解析文件,先处理再输入数据,时间换取空间 两种方法 Scanner scanner1=new Scanner(file1); for(; ...

  7. C/C++安全编码-字符串

    1 字符串     1.1 字符串基础 字符串提供命令行参数.环境变量.控制台输入.文本文件及网络连 接,提供外部输入方法来影响程序的行为和输出,这也是程序容易出错的地方.字符串是一个概念,并不是C/ ...

  8. android JNI (二) 第一个 android工程

    下载NDK 后 它自带有 sample,初学者 可以导入Eclipse 运行 这里 我是自己创建的一个新工程 第一步: 新建一个Android工程 jni_test(名字自取) 第二步:为工程添加 本 ...

  9. DEDE常见问题(转)

    问题1. 把数据保存到数据库附加表 `dede_addonvisa` 时出错,请把相关信息提交给DedeCms官方.Unknown column 'redirecturl' in 'field lis ...

  10. php生成table表格

    function getTable($arrTh, $arrTr){ $s = '<table class="tbData">'; $s .= '<tr>' ...