URAL 2034 Caravans(变态最短路)
Caravans
Memory limit: 64 MB
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.
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
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
Sample
input | output |
---|---|
7 7 |
2 |
Notes
#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(变态最短路)的更多相关文章
- URAL 2034 : Caravans
Description Student Ilya often skips his classes at the university. His friends criticize him for ...
- URAL 1085 Meeting(最短路)
Meeting Time limit: 2.0 secondMemory limit: 64 MB K friends has decided to meet in order to celebrat ...
- URAL 1934 Black Spot(最短路)
Black Spot Time limit: 1.0 secondMemory limit: 64 MB Bootstrap: Jones's terrible leviathan will find ...
- URAL 2069 Hard Rock (最短路)
题意:给定 n + m 个街道,问你从左上角走到右下角的所有路的权值最小的中的最大的. 析:我们只要考虑几种情况就好了,先走行再走列和先走列再走行差不多.要么是先横着,再竖着,要么是先横再竖再横,要么 ...
- URAL
URAL 2035 输入x,y,c, 找到任意一对a,b 使得a+b==c&& 0<=a<=x && 0<=b<=y 注意后两个条件,顺序搞错 ...
- Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)
1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...
- DP/最短路 URAL 1741 Communication Fiend
题目传送门 /* 题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版 正版+破解版->正版,盗版+破解版->盗版 DP:每种情况考虑一 ...
- 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 ...
- URAL 1056 Computer Net(最短路)
Computer Net Time limit: 2.0 secondMemory limit: 64 MB Background Computer net is created by consecu ...
随机推荐
- 离线安装chrome插件
1. 先百度下载自己要安装的chrome插件,比如:Postman 和 Json View. 2.下载完应该得到的是扩展名为crx的文件. 比如: Postman-REST-Client_v0.8.4 ...
- CodeForces 546B C(Contest #1)
Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...
- [转]android Intent机制详解
转自:http://blog.csdn.net/t12x3456/article/details/7688154 1.什么是Intent Intent是一种运行时绑定(run-time binding ...
- java基础-007
41.Servlet Servlet 是处理客户端请求并产生动态网页内容的Java类.Servlet主要是用来处理或者存储HTML表单提交的数据,产生动态内容,在无状态的HTTP协议下管理状态信息.所 ...
- 新版的tomcat里面get请求通过http协议的时候似乎默认是UTF-8的编码了吗?
不在servler.xml的connector中添加URICoding=“UTF-8”,使用默认值一样没有乱码,而添加URICoding=“iso-8859-1”就是乱码了. POST请求还是用iso ...
- EAX、ECX、EDX、EBX寄存器的作用
注意:在计算加法时,实在32位的累加器上进行,并注意类型之间的转换,数据的截取问题 一般寄存器:AX.BX.CX.DXAX:累积暂存器,BX:基底暂存器,CX:计数暂存器,DX:资料暂存器 索引暂存器 ...
- acm 20140825
为了自己的梦想,一次次的选择坚强.走上acm这条路,怎么也找不到让自己放弃的理由.我喜欢这种竞赛的氛围,我渴望在赛场上飞扬!想想过去的一个学习,自己并没有干点什么有意义的事.acm也没有好好的做!新的 ...
- Unknown type name “CGFloat
一.编绎显示Unknown type name “CGFloat” 错误解决方法 将Compile Sources As 改为 Objective-C++ 二.如果是extern const引起的. ...
- ubuntu知识收集
1 就像windows下的环境变量分为系统变量和用户变量一样,ubuntu下也是一样,四种环境变量, 1 系统环境变量:/etc/enviroment, 是整个系统的环境变量,与登录用户无关,在这 ...
- cookie 内容的获取
NSMutableArray *cookiesStoreAll = [[NSMutableArray alloc]init]; NSUInteger totalNumberOfCookies; ...