2016弱校联盟十一专场10.2——Floyd-Warshall
题目链接:Floyd-Warshall
题意:
给你n个点,m条边,100>m-n>0,现在有q个询问,问你任意两点的最短距离,题目保证每条边都被连接,每条边的距离为1
题解:
首先我们可以看到边最多只比点多100个,那么我们可以先将n-1条边生成一棵树,然后用LCA来求最短距离。
然而有可能最短路在多余的这100条边上,所以我们将这100条边的两个端点到所有点的最短路用bfs预处理出来,
然后再用来更新一下答案就行。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define F(i,a,b) for(int i=a;i<=b;i++)
#define mst(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef pair<int,int>P; const int N=1e5+,DEG=;
int g[N],v[N*],nxt[N*],ed,n,m,q,eed,cnt,id[N],Q[N];
int dep[N],fa[N][DEG],dfn[N],idx,d[][N];
P vec[N];
bool vis[N]; inline void adg(int x,int y){v[++ed]=y,nxt[ed]=g[x],g[x]=ed;}
inline void up(int &a,int b){if(a>b)a=b;} void dfs(int u=,int pre=)
{
dep[u]=dep[pre]+,fa[u][]=pre,dfn[u]=++idx;
F(i,,DEG-)fa[u][i]=fa[fa[u][i-]][i-];
for(int i=g[u];i;i=nxt[i])if(!dfn[v[i]])dfs(v[i],u);
else if(v[i]!=pre&&dfn[v[i]]<dfn[u])vec[++eed]=P(u,v[i]);
} int LCA(int a,int b){
if(dep[a]>dep[b])a^=b,b^=a,a^=b;
if(dep[a]<dep[b])F(i,,DEG-)if((dep[b]-dep[a])&(<<i))b=fa[b][i];
if(a!=b)for(int i=DEG-;i<?a=fa[a][]:,i>=;i--)
if(fa[a][i]!=fa[b][i])a=fa[a][i],b=fa[b][i];
return a;
} void bfs(int *d,int S)
{
mst(vis,);
int head=,tail=,now;
Q[++tail]=S,vis[S]=,d[S]=;
while(head<=tail)for(int i=g[now=Q[head++]];i;i=nxt[i])
if(!vis[v[i]])vis[v[i]]=,d[v[i]]=d[now]+,Q[++tail]=v[i];
} int main()
{
while(~scanf("%d%d%d",&n,&m,&q))
{
int x,y;ed=idx=eed=cnt=;
mst(g,),mst(dfn,),mst(fa,),mst(id,);
F(i,,m)scanf("%d%d",&x,&y),adg(x,y),adg(y,x);
dfs();
F(i,,eed)
{
if(!id[vec[i].first])id[vec[i].first]=++cnt,bfs(d[cnt],vec[i].first);
if(!id[vec[i].second])id[vec[i].second]=++cnt,bfs(d[cnt],vec[i].second);
}
F(i,,q)
{
scanf("%d%d",&x,&y);
int ans=dep[x]+dep[y]-*dep[LCA(x,y)];
F(j,,eed)
{
int u=id[vec[j].first],v=id[vec[j].second];
up(ans,d[u][x]+d[v][y]+),up(ans,d[v][x]+d[u][y]+);
}
printf("%d\n",ans);
}
}
return ;
}
2016弱校联盟十一专场10.2——Floyd-Warshall的更多相关文章
- 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)
题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8506#problem/A problem description As we know, t ...
- 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...
- (2016弱校联盟十一专场10.3) D Parentheses
题目链接 把左括号看成A右括号看成B,推一下就行了.好久之前写的,推到最后发现是一个有规律的序列. #include <bits/stdc++.h> using namespace std ...
- (2016弱校联盟十一专场10.3) B.Help the Princess!
题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...
- (2016弱校联盟十一专场10.3) A.Best Matched Pair
题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...
- 2016弱校联盟十一专场10.3---We don't wanna work!(STL--set的使用)
题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8504#problem/C 代码如下: #include <iostream> # ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- (2016弱校联盟十一专场10.2) A.Nearest Neighbor Search
题目链接 水题,算一下就行. #include <bits/stdc++.h> using namespace std; typedef long long ll; ll x[],y[], ...
- (2016弱校联盟十一专场10.2) E.Coins
题目链接 很久之前写的了,好像是对拍打表过的,推一下就行了. #include <bits/stdc++.h> using namespace std; typedef long long ...
- (2016弱校联盟十一专场10.5) F. Fibonacci of Fibonacci
题目链接 题目大意就是这个,先找出下标的循环节,再快速幂对20160519取余就行了. 找出下标循环节: #include <cstdio> #include <iostream&g ...
随机推荐
- 逃出克隆岛 (codevs 2059)
较普通的走迷宫的题 传送门 :codevs 2059 逃出克隆岛 思路 :BFS 即可 PS :传送门 不必重复使用 #include <iostream> #include < ...
- JavaScript - 平稳退化
JavaScript使用window对象的open()方法来创建新的浏览器窗口.这个方法有三个参数:window.open(url,name,features)这三个参数都是可选的.1.第一个参数是想 ...
- delphi 程序输出文件夹存放位置
- log4j基本使用方法
通过配置文件可知,我们需要配置3个方面的内容: 1.根目录(级别和目的地) 2.目的地(控制台和文件等) 3.输出样式 Log4j由三个重要的组件构成: 1.日志信息的优先级 日志信息的优先级从高到低 ...
- Java 彩色图转灰度图
1. 方法1 BufferedImage grayImage = new BufferedImage(width, height, colorImage.TYPE_BYTE_GRAY); Graphi ...
- PHP在浏览器上跟踪调试的方法以及使用ChromePhp、FirePHP的简单介绍
之前用ThinkPHP时发现有个 trace 函数可以跟踪调试,感觉很有意思,网上搜索了下类似的东西,发现了 ChromePhp ,以前没想过这样来调试 PHP 程序,感觉非常方便,很有用. Thin ...
- PHP数组关于数字键名的问题
以下是对PHP数组数字键名的几点总结: 键名长度只能在 int 长度范围内,超过int 范围后将会出现覆盖等混乱情况 在键名长度为 int 范围内存取值时,PHP会强制将数字键名转换为 int 数值型 ...
- 推荐几个在线PDF转化成Word网站
不想安装专业的pdf转换成word软件,希望大家喜欢!昨天用的https://www.pdftoword.com/# 成功搞定! 1.Free-PDFtoWord 在线转换工具: 地址:http:// ...
- CSS 效果汇总
只要决心够, 就能征服痛苦. 把一些常用的 CSS 效果记录下来 1. 利用 z-index :hover 显示层 github 效果地址>> 此效果主要利用 a:hover 来改变 sp ...
- HUD 1171 Big Event in HDU(01背包)
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...