题目链接

先随便建一棵树。

如果两个点(u,v)不经过非树边,它们的dis可以直接算。

如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案。

所以枚举每条非树边的两个端点,求一遍这两个点到所有点的最短路。非树边最多21条,所以要求一遍最短路的点最多42个。

另外对于一条边的两个点只求一个就好了。因为要用这条非树边的话它们两个都要经过。

//779ms	28900KB
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define mp std::make_pair
#define pr std::pair<LL,int>
//#define gc() getchar()
#define MAXIN 250000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e5+5; int Enum,H[N],nxt[N<<1],to[N<<1],len[N<<1],fa[N],dep[N],sz[N],son[N],top[N],sk[N];
LL dist[N],dis[23][N];
bool upd[N],nottree[N];
std::priority_queue<pr> q;
char IN[MAXIN],*SS=IN,*TT=IN; #define AE(u,v,w) to[++Enum]=v,nxt[Enum]=H[u],H[u]=Enum,len[Enum]=w,to[++Enum]=u,nxt[Enum]=H[v],H[v]=Enum,len[Enum]=w
inline int read()
{
int now=0; register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline int LCA(int u,int v)
{
while(top[u]!=top[v]) dep[top[u]]>dep[top[v]]?u=fa[top[u]]:v=fa[top[v]];
return dep[u]>dep[v]?v:u;
}
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void DFS1(int x)
{
int mx=0; sz[x]=1;
for(int v,i=H[x]; i; i=nxt[i])
if(!nottree[i] && (v=to[i])!=fa[x])
{
fa[v]=x, dep[v]=dep[x]+1, dist[v]=dist[x]+len[i], DFS1(v), sz[x]+=sz[v];
if(sz[v]>mx) mx=sz[v], son[x]=v;
}
}
void DFS2(int x,int tp)
{
top[x]=tp;
if(son[x])
{
DFS2(son[x],tp);
for(int i=H[x]; i; i=nxt[i])
if(!nottree[i] && to[i]!=fa[x] && to[i]!=son[x]) DFS2(to[i],to[i]);
}
}
void Dijkstra(LL *dis,int s,int n)
{
static bool vis[N]; memset(vis,0,sizeof vis);
memset(dis,0x3f,sizeof(LL)*(n+1));//dis是指针!
dis[s]=0, q.push(mp(0,s));
while(!q.empty())
{
int x=q.top().second; q.pop();
if(vis[x]) continue;
vis[x]=1;
for(int i=H[x],v; i; i=nxt[i])
if(dis[v=to[i]]>dis[x]+len[i]) q.push(mp(-(dis[v]=dis[x]+len[i]),v));
}
} int main()
{
int n=read(), m=read(); Enum=1;
for(int i=1; i<=n; ++i) fa[i]=i; int tote=0,cnt=0;
for(int i=1,r1,r2,u,v,w; i<=m; ++i)
{
r1=Find(u=read()), r2=Find(v=read()), w=read();
AE(u,v,w);
if(r1==r2) nottree[Enum]=1, nottree[Enum^1]=1, sk[++tote]=Enum;
else fa[r1]=r2;
}
fa[1]=1, DFS1(1), DFS2(1,1);
for(int i=1; i<=tote; ++i)
{
int e=sk[i];
if(!upd[to[e]]) upd[to[e]]=1, Dijkstra(dis[++cnt],to[e],n);
// if(!upd[to[e^1]]) upd[to[e^1]]=1, Dijkstra(dis[++cnt],to[e^1],n);
} for(int Q=read(),u,v; Q--; )
{
u=read(),v=read();
LL ans=dist[u]+dist[v]-(dist[LCA(u,v)]<<1ll);
for(int i=1; i<=cnt; ++i)
ans=std::min(ans,dis[i][u]+dis[i][v]);
printf("%I64d\n",ans);
} return 0;
}

Codeforces.1051F.The Shortest Statement(最短路Dijkstra)的更多相关文章

  1. codeforces 1051F The Shortest Statement

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

  2. 2018.09.24 codeforces 1051F. The Shortest Statement(dijkstra+lca)

    传送门 这真是一道一言难尽的题. 首先比赛的时候居然没想出来正解. 其次赛后调试一直调不出来最后发现是depth传错了. 其实这是一道简单题啊. 对于树边直接lca求距离. 由于非树边最多21条. 因 ...

  3. [Codeforces 1051F] The Shortest Statement 解题报告(树+最短路)

    题目链接: https://codeforces.com/contest/1051/problem/F 题目大意: 给出一张$n$个点,$m$条边的带权无向图,多次询问,每次给出$u,v$,要求输出$ ...

  4. Codeforces 1051E Vasya and Big Integers&1051F The Shortest Statement

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

  5. Codeforces.567E.President and Roads(最短路 Dijkstra)

    题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...

  6. Codeforces Gym101502 I.Move Between Numbers-最短路(Dijkstra优先队列版和数组版)

    I. Move Between Numbers   time limit per test 2.0 s memory limit per test 256 MB input standard inpu ...

  7. Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...

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

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

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

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

随机推荐

  1. Linux内存管理2---段机制

    1.前言 本文所述关于内存管理的系列文章主要是对陈莉君老师所讲述的内存管理知识讲座的整理. 本讲座主要分三个主题展开对内存管理进行讲解:内存管理的硬件基础.虚拟地址空间的管理.物理地址空间的管理. 本 ...

  2. UML和模式应用5:细化阶段(7)---从需求到设计迭代进化

    1.前言 迭代开发中,每次迭代都会发生从以需求或分析为主要焦点到以设计和实现为主要焦点的转变 分析和面向对象的分析重点关注学习做正确的事,理解案例重要目标,规则和约束 设计工作强调正确的做事,熟练设计 ...

  3. springboot系列五、springboot常用注解使用说明

    一.controller相关注解 1.@Controller 控制器,处理http请求. 2.@RespController Spring4之后新加的注解,原来返回json需要@ResponseBod ...

  4. ThinkPHP使用不当可能造成敏感信息泄露

    ThinkPHP在开启DEBUG的情况下会在Runtime目录下生成日志,如果debug模式不关,可直接输入路径造成目录遍历. ThinkPHP3.2结构:Application\Runtime\Lo ...

  5. concat layer

    参考:http://blog.csdn.net/bailufeiyan/article/details/50876728#reply

  6. nginx配置学习总结

    1.nginx反向代理 在讲诉具体的配置之前,先说下正向代理与反向代理的区别. 正向代理:是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理 ...

  7. 如何将Request对象中的参数列表打印出来

    Map<String, String[]> map = request.getParameterMap(); Set<Map.Entry<String, String[]> ...

  8. Laravel collection 报错 join(): Invalid arguments passed

    混淆了 array 与 collection,join 并不支持 collection. array 与 collection 不同的 join 实现 collect([1, 2, 3, 4, 5]) ...

  9. eclipse中Ruby环境搭建

    用Eclipse学习Watir.Eclipse支持Ruby的插件:RDT(Ruby Development Tools),下载下来试用了一下,感觉还是不错的.第一步:获取RDT,通过以下链接可以获得R ...

  10. ERP商品类型管理相关业务处理(三十五)

    根据类型编号获取父类名称 -- ============================================= CREATE FUNCTION [dbo].[FN_getParentTyp ...