题目大意:给定一张$n$个点$m$条有权边的无向联通图,$q$次询问两点间的最短路

$n\le100000$,$m\le100000$,$1\le100000$,$m$-$n\le20$.

首先看到$m$-$n\le20$这条限制,我们可以想到是围绕这个20来做这道题。

即如果我们随便在图上找一棵树,有最多21条非树边,连接最多42个顶点

考虑两点$x,y$之间的最短路就是某个点到$x$和$y$的最短路之和

首先对于只走树边的情况,这个点是两点的$LCA$

如果经过非树边,$x$或$y$到枚举的这个点的最短路上的最后一条边一定是非树边(如果都是树边的话完全可以转化到一个连接着非树边的点上去)

然后对于所有连接非树边的点都跑一遍最短路,询问时直接枚举这些点取$min$即可

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define M 200010
#define int long long
using namespace std;
int read()
{
char ch=getchar();int x=;
while(ch>''||ch<'') ch=getchar();
while(ch<=''&&ch>='') x=x*+ch-'',ch=getchar();
return x;
}
struct point{
int to,next,dis;
}e[M<<];
int n,m,num,Q,top;
int q[M],head[M],deep[M],dist[M];
int dis[][M],fa[M][];
bool vis[M];
struct node{int id,dis;};
bool operator < (node a1,node a2) {return a1.dis>a2.dis;}
void add(int from,int to,int dis)
{
e[++num].next=head[from];
e[num].to=to;
e[num].dis=dis;
head[from]=num;
}
void dfs(int x,int f)
{
vis[x]=true; fa[x][]=f;
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
if(to==f) continue;
if(vis[to]) q[++top]=x,q[++top]=to;
else
{
deep[to]=deep[x]+;
dist[to]=dist[x]+e[i].dis;
dfs(to,x);
}
}
}
int lca(int x,int y)
{
if(deep[x]<deep[y]) swap(x,y);
for(int i=;i>=;i--)
if(deep[fa[x][i]]>=deep[y])
x=fa[x][i];
if(x==y) return x;
for(int i=;i>=;i--)
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
return fa[x][];
}
void Dijkstra(int id)
{
memset(dis[id],,sizeof(dis[id]));
memset(vis,false,sizeof(vis));
priority_queue<node>Q;
dis[id][q[id]]=;
Q.push((node){q[id],});
while(!Q.empty())
{
int x=Q.top().id;Q.pop();
if(vis[x]) continue;
vis[x]=true;
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
if(!vis[to]&&dis[id][x]+e[i].dis<dis[id][to])
{
dis[id][to]=dis[id][x]+e[i].dis;
Q.push((node){to,dis[id][to]});
}
}
}
}
#undef int
int main()
{
#define int long long
n=read(); m=read();
for(int i=;i<=m;i++)
{
int a=read(),b=read(),c=read();
add(a,b,c); add(b,a,c);
}
deep[]=;dfs(,);
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
sort(q+,q++top); top=unique(q+,q++top)-q-;
for(int i=;i<=top;i++) Dijkstra(i);
Q=read();
while(Q--)
{
int x=read(),y=read();
int ans=dist[x]+dist[y]-*dist[lca(x,y)];
for(int i=;i<=top;i++) ans=min(ans,dis[i][x]+dis[i][y]);
printf("%lld\n",ans);
}
return ;
}

[CF1051F]The Shortest Statement的更多相关文章

  1. 【题解】Luogu CF1051F The Shortest Statement

    原题传送门:CF1051F The Shortest Statement 题目大意,给你一个稀疏图,q次查询,查询两点之间距离 边数减点小于等于20 这不是弱智题吗,23forever dalao又开 ...

  2. cf1051F. The Shortest Statement(最短路/dfs树)

    You are given a weighed undirected connected graph, consisting of nn vertices and mm edges. You shou ...

  3. CF1051F The Shortest Statement 题解

    题目 You are given a weighed undirected connected graph, consisting of n vertices and m edges. You sho ...

  4. [CF1051F]The Shortest Statement (LCA+最短路)(给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路)

    题目:给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路 n≤100000,m≤100000,m-n≤20. 首先看到m-n≤20这条限制,我们可以想到是围绕这个20来做这道题. 即如果我们 ...

  5. cf1051F. The Shortest Statement(最短路)

    题意 题目链接 题意:给出一张无向图,每次询问两点之间的最短路,满足$m - n <= 20$ $n, m, q \leqslant 10^5$ Sol 非常好的一道题. 首先建出一个dfs树. ...

  6. CF1051F The Shortest Statement Dijkstra + 性质分析

    动态询问连通图任意两点间最短路,单次询问. 显然,肯定有一些巧妙地性质(不然你就发明了新的最短路算法了233)有一点很奇怪:边数最多只比点数多 $20$ 个,那么就可以将这个图看作是一个生成树,上面连 ...

  7. codeforces 1051F The Shortest Statement

    题目链接:codeforces 1051F The Shortest Statement 题意:\(q\)组询问,求任意两点之间的最短路,图满足\(m-n\leq 20\) 分析:一开始看这道题:fl ...

  8. The Shortest Statement CodeForces - 1051F(待测试)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  9. Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路

    F - The Shortest Statement emmm, 比赛的时候没有想到如何利用非树边. 其实感觉很简单.. 对于一个询问答案分为两部分求: 第一部分:只经过树边,用倍增就能求出来啦. 第 ...

随机推荐

  1. 后台程序在向tty/串口写数据的时候stop了

    当后台程序向tty/串口写数据的时候stop了. STOPPED(SIGTTOU) .... SIGTTOU:代表的是后台程序向 controlling terminal写数据. 解决办法:暂时在程序 ...

  2. 20165330 预备作业3 Linux安装及学习

    虚拟机安装 在安装VirtualBox时我的电脑一直打不开官网的下载地址,还好后面有可以打开了,于是我顺利的下载好了VirtualBox.而在运行出现了以下错误: 错误1:点击创建虚拟机时出现了以下提 ...

  3. Net 常用资源

    opensource: http://www.dotnetfoundation.org/projects https://github.com/dotnet/corefx Enterprise Lib ...

  4. Graphite grafana

    Graphite http://graphiteapp.org/ Graphite is an enterprise-ready monitoring tool that runs equally w ...

  5. TCP控制位 sendUrgentData 队列 队列元素 优先级 极限 急停 置顶

    Socket (Java Platform SE 7 ) https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#sendUrge ...

  6. DEV中gridview常用属性

    1.隐藏最上面的GroupPanel: gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值: sValue=Table.Rows[g ...

  7. vue下使用echarts折线图及其横坐标拖拽功能

    vue页面中使用折线图,并且有时间段筛选.因此就需要用到横坐标的拖拽功能. 界面效果如下: 现在来看这个效果的实现代码: drawLine() { let that = this, lineDate ...

  8. 安装canal

    一.安装前准备 配置Mysql: [mysqld] log-bin=mysql-bin #添加这一行就ok binlog-format=ROW #选择row模式 server_id=1 #配置mysq ...

  9. 部署knight项目

    发布CRM你将使用以下软件 nginx uWSGI CentOS7 CRM项目文件 virtualenv supervisor WSGI.uWSGI python web服务器开发使用WSGI协议(W ...

  10. flatpickr功能强大的日期时间选择器插件

    flatpickr日期时间选择器支持移动手机,提供多种内置的主题效果,并且提供对中文的支持.它的特点还有: 使用SVG作为界面的图标. 兼容jQuery. 支持对各种日期格式的解析. 轻量级,高性能, ...