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时所耗时...... ...
随机推荐
- DML-插入
插入的方式一: 语法: insert into 表名(字段)values(值); 特点: 1.要求表明括号里的属性和values括号里的属性一致或兼容 2.字段的个数和顺序不一定与原始表中的字段个数和 ...
- 更新Android Studio 3.1.1碰到的问题
碰到了如下问题 The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin. Perhaps you m ...
- flex学习园地
http://blog.sina.com.cn/s/blog_6d0dc2a901013enk.html
- 2017-2018-1 20155331 课下测试(ch10)
2017-2018-1 20155331 课下测试(ch10) 假设下面代码中的foobar.txt中有6个ASCII字母,程序的输出是(A) Image 7.png A . c = f B . c ...
- 查内存命令之free
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackgao@gmail. ...
- Tarjan/2-SAT学习笔记
Tarjan/2-SAT Tags:图论 作业部落 评论地址 Tarjan 用来求割边或者割点,求点双联通分量或者边双联通分量 点双联通分量:两个点之间有两条点不相交的路径 边双联通分量:两个点之间有 ...
- c++ 创建二叉树
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...
- c++ 动态数组,指针与动态内存分配
教学内容: 内存的使用 动态内存分配malloc函数 分配内存时使用sizeof运算符 用指针访问内存 以数组的形式访问内存 一.内存的使用 堆(heap) 在程序执行期间分配内存时,内存区域中的这个 ...
- 洛谷 P2563 [AHOI2001]质数和分解
洛谷 P2563 [AHOI2001]质数和分解 题目描述 任何大于 1 的自然数 n 都可以写成若干个大于等于 2 且小于等于 n 的质数之和表达式(包括只有一个数构成的和表达式的情况),并且可能 ...
- JMeter入门教程
转自 http://blog.csdn.net/w565911788/article/details/7629787 1.Jmeter 概要描叙 jmeter 是一款专门用于功能测试和压力测试的轻量级 ...