原题传送门:CF1051F The Shortest Statement

题目大意,给你一个稀疏图,q次查询,查询两点之间距离

边数减点小于等于20

这不是弱智题吗,23forever dalao又开始虐题

作为蒟蒻的我只能在一旁出售烤绿鸟和main包,和大家一起吃西瓜

仔细想想,这题的确是很弱智

先随便找一个生成树,这样就能跑lca了

剩下的几条边的端点跑一下SPFA堆优化dij,用于特判,SPFA已经死了

查询先用lca算一下距离,再暴力枚举这40个端点到两点的距离值和(最多)

就这样完了,没错

程序中还有一些优化,看细节来体会(这题真的弱智)

奉上蒟蒻的代码

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define N 300005
#define Logn 19
#define M 21
#define inf 1e18
#define ll long long
using namespace std;
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline ll Min(register ll a,register ll b)
{
return a<b?a:b;
}
int n,m,q;
struct edge{
int to,next,w;
}e[N<<1];
int head[N],cnt=0;
set< pair<int,int> >bad;
int tin[N],tout[N],T;
bool u[N];
ll h[N],d[M<<1][N];
int p[Logn][N];
inline void add(register int u,register int v,register int w)
{
e[++cnt]=(edge){v,head[u],w};
head[u]=cnt;
}
inline void dfs(register int v,register int pr)
{
tin[v]=T++;
p[0][v]=pr;
u[v]=true;
for(register int i=1;i<Logn;++i)
p[i][v]=p[i-1][p[i-1][v]];
for(register int i=head[v];i;i=e[i].next)
if(!u[e[i].to])
{
h[e[i].to]=h[v]+e[i].w;
dfs(e[i].to,v);
if(v<e[i].to)
bad.erase(make_pair(v,e[i].to));
else
bad.erase(make_pair(e[i].to,v));
}
tout[v]=T;
}
inline bool isAncestor(register int a,register int b)
{
return tin[a]<=tin[b]&&tout[a]>=tout[b];
}
inline int LCA(register int a,register int b)
{
if(isAncestor(a,b))
return a;
if(isAncestor(b,a))
return b;
for(register int i=Logn-1;i>=0;--i)
if(!isAncestor(p[i][a],b))
a=p[i][a];
return p[0][a];
}
inline void dij(register int st,register ll d[N])
{
set<pair<ll,int> > q;
for(register int i=0;i<n;++i)
d[i]=inf;
d[st]=0;
q.insert(make_pair(d[st],st));
while(!q.empty())
{
int v=q.begin()->second;
q.erase(q.begin());
for(register int i=head[v];i;i=e[i].next)
if(d[e[i].to]>d[v]+e[i].w)
{
q.erase(make_pair(d[e[i].to],e[i].to));
d[e[i].to]=d[v]+e[i].w;
q.insert(make_pair(d[e[i].to],e[i].to));
}
}
}
int main()
{
n=read(),m=read();
for(register int i=1;i<=m;++i)
{
int u=read(),v=read(),w=read();
--u,--v;
add(u,v,w),add(v,u,w);
}
for(register int v=0;v<n;++v)
for(register int i=head[v];i;i=e[i].next)
if(v<e[i].to)
bad.insert(make_pair(v,e[i].to));
dfs(0,0);
int cpos=0;
int siz=bad.size();
while(!bad.empty())
dij(bad.begin()->first,d[cpos++]),bad.erase(bad.begin());
q=read();
while(q--)
{
int u=read(),v=read();
--u,--v;
int lca=LCA(u,v);
ll ans=h[u]+h[v]-2*h[lca];
for(register int i=0;i<siz;++i)
ans=Min(ans,d[i][u]+d[i][v]);
printf("%lld\n",ans);
}
return 0;
}

【题解】Luogu CF1051F The Shortest Statement的更多相关文章

  1. CF1051F The Shortest Statement 题解

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

  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

    题目大意:给定一张$n$个点$m$条有权边的无向联通图,$q$次询问两点间的最短路 $n\le100000$,$m\le100000$,$1\le100000$,$m$-$n\le20$. 首先看到$ ...

  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 1051E Vasya and Big Integers&1051F The Shortest Statement

    1051E. Vasya and Big Integers 题意 给出三个大整数\(a,l,r\),定义\(a\)的一种合法的拆分为把\(a\)表示成若干个字符串首位相连,且每个字符串的大小在\(l, ...

  8. [CF1051F]The Shortest Statement_堆优化dij_最短路树_倍增lca

    The Shortest Statement 题目链接:https://codeforces.com/contest/1051/problem/F 数据范围:略. 题解: 关于这个题,有一个重要的性质 ...

  9. codeforces 1051F The Shortest Statement

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

随机推荐

  1. CentOS6.5安装Tomcat8.0

    1.首先从官网下载最新的安装包 http://tomcat.apache.org/  apache-tomcat-8.0.20.tar.gz 2.上传安装包到 /usr/local/mypackage ...

  2. JAVA编程思想学习笔记2-chap4-6-斗之气2段

    1.foreach:只能用于数组与容器 2.this指针:内部有个指针指向自己 3.super指针:内部有个指针指向父类部分 4.方法存放于代码区:方法调用时,a.fun()可能会被转换为fun(a) ...

  3. cocos2d-X JS 获取cocostudio中的UI组件

    1.先加载cocostudio导出的json文件,代码如下所示: var dong = ccs.load("res/Login.json"); //_login.setPositi ...

  4. cocos2d-x JS 弹出对话框触摸监听(吞噬点击事件遮挡层)

    在游戏中,我们经常会碰到一些弹窗,这些弹窗禁止点透,也就是禁止触摸事件传递到底层,我们称之为遮挡层,这些遮挡层,需要开发遮挡层,我们首先得了解cocos2d-js的触摸传递机制. 根据官方文档,我们可 ...

  5. model.addattribute()的作用

    1.往前台传数据,可以传对象,可以传List,通过el表达式 ${}可以获取到, 类似于request.setAttribute("sts",sts)效果一样. 2.@ModelA ...

  6. LeetCode112.路径总和

    给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22 ...

  7. pandas常用函数

    1. df.head(n): 显示数据前n行,不指定n,df.head则会显示所有的行 2. df.columns.values获取所有列索引的名称 3. df.column_name: 直接获取列c ...

  8. 删除SQL Server大容量日志的方法(转)

    删除SQL Server大容量日志的方法 亲自实践的方法 1.分享数据库,如果提示被其他连接占用,不能分离,刚勾上drop connections 2.复制下所有文件,一定要备份好,以防自己操作失误 ...

  9. C语言---选择结构和循环结构

    C语言的两种选择语句,1) if语句; 2) switch语句; 在if判断语句中,一般使用关系表达式. 关系运算符: <.<=.>.>=.==.!= 关系表达式:用关系运算符 ...

  10. ubuntu安装python-mysqldb

    前期准备: sudo apt-get install  libmysqld-dev sudo apt-get install libmysqlclient-dev sudo apt-get insta ...