2016弱校联盟十一专场10.2——Floyd-Warshall

时间:2022-04-13 07:04:17

题目链接: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的更多相关文章

  1. 2016弱校联盟十一专场10&period;5---As Easy As Possible(倍增)

    题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8506#problem/A problem description As we know, t ...

  2. 2016弱校联盟十一专场10&period;3---Similarity of Subtrees(深搜&plus;hash、映射)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...

  3. &lpar;2016弱校联盟十一专场10&period;3&rpar;&Tab;D&Tab;Parentheses

    题目链接 把左括号看成A右括号看成B,推一下就行了.好久之前写的,推到最后发现是一个有规律的序列. #include <bits/stdc++.h> using namespace std ...

  4. &lpar;2016弱校联盟十一专场10&period;3&rpar; &Tab;B&period;Help the Princess&excl;

    题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...

  5. &lpar;2016弱校联盟十一专场10&period;3&rpar; &Tab;A&period;Best Matched Pair

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...

  6. 2016弱校联盟十一专场10&period;3---We don&&num;39&semi;t wanna work&excl;(STL--set的使用)

    题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8504#problem/C 代码如下: #include <iostream> # ...

  7. 2016弱校联盟十一专场10&period;2---Around the World(深搜&plus;组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  8. &lpar;2016弱校联盟十一专场10&period;2&rpar; &Tab;A&period;Nearest Neighbor Search

    题目链接 水题,算一下就行. #include <bits/stdc++.h> using namespace std; typedef long long ll; ll x[],y[], ...

  9. &lpar;2016弱校联盟十一专场10&period;2&rpar; &Tab;E&period;Coins

    题目链接 很久之前写的了,好像是对拍打表过的,推一下就行了. #include <bits/stdc++.h> using namespace std; typedef long long ...

  10. &lpar;2016弱校联盟十一专场10&period;5&rpar; F&period; Fibonacci of Fibonacci

    题目链接 题目大意就是这个,先找出下标的循环节,再快速幂对20160519取余就行了. 找出下标循环节: #include <cstdio> #include <iostream&g ...

随机推荐

  1. 用Redis实现Session功能

    0.什么是Redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API ---* 1.与其他用户状态保存方 ...

  2. JQuery 对 Select option 的操作

    下拉框: <select id="selectID" >         <option value="1">1</option& ...

  3. EffectiveJava笔记(第一部分)

    考虑用静态构造方法代替构造器的好处: 1.静态构造方法有名字     BigInteger.probablePrime(int, int, Random)比 new BigInteger(int, i ...

  4. tomcat https 未测试成功的版本

  5. C&num;中System&period;Globalization&period;DateTimeFormatInfo&period;InvariantInfo怎么用

    原文  C#中System.Globalization.DateTimeFormatInfo.InvariantInfo怎么用 在开发的时候,碰到下面这样一个问题: 在程序中显示当前系统时间,但是有一 ...

  6. Linux~Archer 进化之路

    使用过的linux系统有:Redhat.红旗Linux.Deepin.Ubuntu.Debian.Fedora.Kali.Parrot.manjaro.Mint.Arch,最早接触linux是从200 ...

  7. Centos7的防火墙关闭

    第一步.centos7安装service 第二步. 或者可以不用service,有另一个办法.

  8. Mqtt用户认证

    http://emqtt.com/docs/v2/guide.html 1默认是匿名认证,不用输入用户名和密码,直接可连接 2如何开启用户名和密码认证模式 2-1关闭匿名认证 在你的MQTT安装目录下 ...

  9. js &amp&semi; float number bug

    js & float number bug 前端最好不要处理任何的 float number 的计算/精确度转换的操作,不热很容易丢失精度,显示错误! 前端显示个 0.0 都很费劲,最好的方式 ...

  10. 为什么TCP比UDP可靠真正原因,以及并发编程的基础问题

    一  为什么TCP协议比UDP协议传输数据可靠: 我们知道在传输数据的时候,数据是先存在操作系统的缓存中,然后发送给客户端,在客户端也是要经过客户端的操作系统的,因为这个过程涉及到计算机硬件,也就是物 ...