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时所耗时...... ...
随机推荐
- JS基础-组成
类型 前缀 类型 实例 数组 a Array aItems 布尔值 b Boolean bIsComplete 浮点数 f Float fPrice 函数 fn Function fnHandler ...
- adb devices报错解决
1. 执行adb device报错如下 2. 报错原因及解决办法 报错时开启了Androidkiller,关闭即解决问题 可能原因:adb命令被占用冲突了
- ACM1001:Sum Problem
Problem Description In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. Input ...
- Django model字段类型(转)
AutoField 一个 IntegerField, 添加记录时它会自动增长. 你通常不需要直接使用这个字段; 如果你不指定主键的话,系统会自动添加一个主键字段到你的 model.(参阅 _自 ...
- VB6 加载水晶报表例子
VB6 加载水晶报表例子 先按照水晶报表组件 Crystal Reports,Business Objects,现已被SAP收购. 再添加引用 'Library: CRAXDRT 'C:\Progra ...
- 基于bootstrap的文本编辑器组件:Summernote
Summernote官网地址 :https://summernote.org/ 这是官网的一个例子: <!DOCTYPE html> <html lang="en" ...
- 【转载】怎样在C++工程中集成C#窗口
原文:http://www.cnblogs.com/clever101/archive/2009/12/14/1624204.html 本文转自博客园,此文作者依据codeproject英文版本翻译! ...
- 函数内联inline
C++语言支持函数内联,其目的是为了提高函数的执行效率(速度). 宏的优点 在C程序中,可以用宏代码提高执行效率. 编译预处理器用拷贝宏代码的方式取代函数调用,省去了参数压栈,生成汇编语言的CALL调 ...
- 9 ORM-高阶补充(未完成)
https://www.cnblogs.com/alice-bj/p/9195846.html#_labelTop https://www.cnblogs.com/yuanchenqi/article ...
- 【HNOI2015】菜肴制作
题面 题解 这道题目首先可以想到拓扑排序,但是肯定不是字典序最小的排列. 比如说,有\(4\)种菜,限制为\(2 \to 4, 3 \to 1\),那么如果求字典序最小的排列会算出\((2, 3, 1 ...