BNUOJ 52303 Floyd-Warshall Lca+bfs最短路
题目链接:
https://www.bnuoj.com/v3/problem_show.php?pid=52303
Floyd-Warshall
Time Limit: 60000msMemory Limit: 1048576KB
#### 问题描述
> In ICPCCamp, there are n cities and m (bidirectional) roads between cities. The i-th road is between the
> ai-th city and the bi-th city. There may be roads connecting a citie to itself and multiple roads between
> the same pair of cities.
> Bobo has q travel plans. The i-th plan is to travel from the ui-th city to the vi-th city. He would like
> to know the smallest number of roads needed to travel for each plan. It is guaranteed that cities are
> connected.
输入
The first line contains 3 integers n, m, q (1 ≤ n ≤ 105
, 0 < m − n < 100, 1 ≤ q ≤ 105
).
The i-th of the following m lines contains 2 integers ai
, bi (1 ≤ ai
, bi ≤ n).
The i-th of the last q lines contains 2 integers ui
, vi (1 ≤ ui
, vi ≤ n).
输出
n lines with integers l1, l2, . . . , ln. li denotes the smallest number of roads travelling from city ui to city
vi
.
样例输入
4 5 3
1 2
1 3
1 4
2 3
3 4
2 2
2 3
2 4
样例输出
0
1
2
题意
给你n个点,m条无相边,q个询问,查询u到v的最短路。
题解
首先n有105,所以n2的bfs暴力每个源点是不可能的。
那么这题有什么特点呢?m-n<100,明显是张稀疏图!
如果题目给的是一颗树,那么我们就可以用logn的算法处理一个询问。(倍增LCA)
因此我们可以考虑把图拆出一颗树(其实是森林),吧所有非树边的点都存起来,对它们跑bfs求到所有其他点的最短路。
对于一个查询,首先我们先用logn求只经过树边的最短路,然后暴力枚举会进过的一个非树边上的顶点x,用d(u,x)+d(x,v)来更新答案。 这样就处理完了。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
//数据需要开大点,否则会出现tle、re的情况。。。
const int maxn=101010;
const int maxm=18;
struct Edge {
int u,v,ne,flag;
Edge(int u,int v,int ne):u(u),v(v),ne(ne) {
flag=0;
}
Edge() {}
} egs[maxn*2];
int head[maxn],tot;
int used[maxn];
void addEdge(int u,int v) {
egs[tot]=Edge(u,v,head[u]);
head[u]=tot++;
}
//dep:深度,fa:父亲,anc:祖先
int dep[maxn],fa[maxn],vis[maxn];
int anc[maxn][maxm];
void dfs(int u,int f,int d) {
vis[u]=1;
dep[u]=d;
fa[u]=f;
anc[u][0]=f;
for(int i=1; i<maxm; i++) {
anc[u][i]=anc[anc[u][i-1]][i-1];
}
for(int p=head[u]; p!=-1; p=egs[p].ne) {
Edge& e=egs[p];
if(e.v==f) continue;
if(!vis[e.v]) {
dfs(e.v,u,d+1);
} else {
used[u]=used[e.v]=1;
}
}
}
//dis:非树边上的点到所有点的距离
int dis[222][maxn];
//ha:存非树边上的点
int ha[222],last;
int myque[maxn],frt,rear;
void bfs(int id) {
int s=ha[id];
frt=0,rear=0;
dis[id][s]=0;
myque[rear++]=s;
clr(vis,0);
vis[s]=1;
while(frt<rear) {
int u=myque[frt++];
for(int p=head[u]; p!=-1; p=egs[p].ne) {
Edge& e=egs[p];
if(vis[e.v]) continue;
vis[e.v]=1;
dis[id][e.v]=dis[id][u]+1;
myque[rear++]=e.v;
}
}
}
int Lca(int u,int v) {
if(dep[u]<dep[v]) swap(u,v);
for(int i=maxm-1; i>=0; i--) {
if(dep[anc[u][i]]>=dep[v]) {
u=anc[u][i];
}
}
if(u==v) return u;
for(int i=maxm-1; i>=0; i--) {
if(anc[u][i]!=anc[v][i]) {
u=anc[u][i];
v=anc[v][i];
}
}
return anc[u][0];
}
int n,m,q;
int main() {
scf("%d%d%d",&n,&m,&q);
clr(used,0);
clr(head,-1);
tot=0;
//建图
for(int i=0; i<m; i++) {
int u,v;
scf("%d%d",&u,&v);
if(u==v) continue;
addEdge(u,v);
addEdge(v,u);
}
//处理出树边、非树边
clr(vis,0);
clr(anc,0);
for(int i=1; i<=n; i++) {
if(!vis[i]) {
dfs(i,0,1);
}
}
//把非树边上的点存起来
last=0;
for(int i=1; i<=n; i++) if(used[i]) {
ha[last++]=i;
}
//跑非树边上的点到所有点的距离
clr(dis,0x3f);
rep(i,0,last) bfs(i);
while(q--) {
int u,v;
scf("%d%d",&u,&v);
int lca=Lca(u,v);
LL res=dep[u]+dep[v]-2*dep[lca];
rep(i,0,last) {
LL tmp=(LL)dis[i][u]+dis[i][v];
res=min(res,tmp);
}
prf("%lld\n",res);
}
return 0;
}
//end-----------------------------------------------------------------------
BNUOJ 52303 Floyd-Warshall Lca+bfs最短路的更多相关文章
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- 图论之最短路径(1)——Floyd Warshall & Dijkstra算法
开始图论学习的第二部分:最短路径. 由于知识储备还不充足,暂时不使用邻接表的方法来计算. 最短路径主要分为两部分:多源最短路径和单源最短路径问题 多源最短路径: 介绍最简单的Floyd Warshal ...
- 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路
题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...
- 【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流
题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...
- BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)
BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...
- UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)
题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...
- 关于SPFA Bellman-Ford Dijkstra Floyd BFS最短路的共同点与区别
关于模板什么的还有算法的具体介绍 戳我 这里我们只做所有最短路的具体分析. 那么同是求解最短路,这些算法到底有什么区别和联系: 对于BFS来说,他没有松弛操作,他的理论思想是从每一点做树形便利,那么时 ...
- Floyd —Warshall(最短路及其他用法详解)
一.多元最短路求法 多元都求出来了,单源的肯定也能求. 思想是动态规划的思想:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设Dis(A ...
- POJ 3311 Hie with the Pie (BFS+最短路+状态压缩)
题意:类似于TSP问题,只是每个点可以走多次,求回到起点的最短距离(起点为点0). 分析:状态压缩,先预处理各点之间的最短路,然后sum[i][buff]表示在i点,状态为buff时所耗时...... ...
随机推荐
- 使用Jquery Viewer 展示图片信息
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...
- Flume的一些报错问题解决(持续更新中)
严谨转载--否则追究法律责任 作者----王加鸿 ----------bug 1---------- ...
- 【Mac 快捷键】 (不定时持续更新)
显示/取消显示隐藏文件 command + shift + .
- Spring Boot:项目打包成war并发布到Tomcat上运行
一.修改pom文件 1. 因为SpringBoot中嵌入的有Tomcat,因此要移除嵌入式的Tomcat插件 <dependency> <groupId>org.springf ...
- 给浏览器和各种软件配置 http https socks5 代理 proxy
只需要像在 idea 里一样,配置好sr的本地代理ip:127.0.0.1 本地代理端口:1080 浏览器和软件的流量即可走 sr ,就能被 sr 代理了 用上代理之后,确实快了好多,这里是在打开 ...
- 一款好看的Sublime Text浅色主题:Ayu大作
上一篇分享的VS Code的文中,界面也是浅色主题,也是Ayu作品.下面看一下Sublime Text中的Ayu浅色主题 不错吧. 如何下载? 首选项——插件控制——安装插件或者ctrl+shift+ ...
- h5小球走迷宫小游戏源码
无意中找到的一个挺有意思的小游戏,关键是用h5写的,下面就分享给大家源码 还是先来看小游戏的截图 可以用键盘的三个键去控制它,然后通关 下面是源代码 <!doctype html> < ...
- Magic Trackpad 2 on win10 x64
可以使用操作: 触击 触击拖拽 双击 支手滚动:上下,左右 右键配置:左下角,右下角点击实现 可以试用28天,收费 $9.5 官方地址:http://www.trackpadmagic.com/mag ...
- TPO-22 C2 Revise a music history paper
第 1 段 1.Listen to part of a conversation between a student and his music history professor. :听一段学生和音 ...
- 用可道云kodexplorer在dedecms系统网站上秒建私人网盘
国内草根站长用的最多的一款建站源程序就是dedecms,通常是通过FTP或者服务器面板自带的文件管理器来上传下载的.FTP可视性.体验都相对差一点,且需要事先安装FTP软件,更换环境后的站点管理上有很 ...