原题传送门: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. React对比Vue(06 路由的对比)

    其实差不多, 都需要先安装路由 React  先安装 cnpm install react-router-dom --save 在再根组件引入 import { BrowserRouter as Ro ...

  2. 记录一则expdp任务异常处理案例

    环境:AIX 6.1 + Oracle 10.2.0.4 现象:在XTTS迁移测试阶段,遇到执行几个expdp的导出任务,迟迟没有返回任何信息,对应日志无任何输出,查看任务状态: SQL> se ...

  3. vuex使用一

    至于为什么使用vuex在这里就不过多的解释了,直接进入正题 1.vuex的安装 cnpm install vuex -S 2.然后在main.js中引入 import Vue from 'vue' i ...

  4. whu 643 Soul Artist(二维BIT 区间更新,单点查询)

    Soul Artis [题目链接]Soul Artis [题目类型]二维BIT &题解: 二维区间更新和一维相比,要容斥一下,更新一块区间就是更新4个点. 还有这个我先是写了2*n^2logn ...

  5. python 定义函数 两个文件调用函数

    在def_function.py文件里面写 #coding=utf-8 #定义函数 def hello(): print "hello world" 在test.py里面调用 #c ...

  6. jQuery-数据管理-删除事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. oracle中实现md5加密

    记得要实现md5加密,在oracle 11g 和 12c中是有不同的方式的,在12c中较为简单,记得直接有预定义的函数. 但是在11g中要实现就需要自己进行一些额外的处理,以下给出一个md5函数的实现 ...

  8. MD5解密

      国外http://md5.rednoize.com/http://www.milw0rm.com/md5/list.php国内http://www.neeao.com/md5/http://www ...

  9. javascript(一):javascript基本介绍及基本语法

    什么是javascript? javascript是一种直译型脚本语言,是一种动态类型.弱类型.基于原型的语言.(所谓“脚本语言”:指的是它不具有开发操作系统的能力,只是用来编写大型运用程序的脚本!) ...

  10. python 可视化

    一.环境安装 windows:pip install numpy scipy matplotlib #pip install http://effbot.org/downloads/Imaging-1 ...