标签:problem first lld fir -- get print next \n
#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
标签:problem first lld fir -- get print next \n
原文地址:https://www.cnblogs.com/yzhang-rp-inf/p/9741026.html