BZOJ2125 最短路

题目链接
先构造圆方树,然后圆-圆边的距离就是直接距离,圆-方边的距离就是圆点到环的起始点的距离.
然后在圆方树上倍增就好了.
注意如果\(LCA\)是方点就往下跳到圆点,然后做一个环上最短路即可.

代码如下

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N (30010)
#define M (400010)
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
inline char read(){
    static const int IN_LEN=1000000;
    static char buf[IN_LEN],*s,*t;
    return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
}
template<class T>
inline void read(T &x){
    static bool iosig;
    static char c;
    for(iosig=false,c=read();!isdigit(c);c=read()){
        if(c=='-')iosig=true;
        if(c==-1)return;
    }
    for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');
    if(iosig)x=-x;
}
inline char readchar(){
    static char c;
    for(c=read();!isalpha(c);c=read())
    if(c==-1)return 0;
    return c;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN],*ooh=obuf;
inline void print(char c) {
    if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
    *ooh++=c;
}
template<class T>
inline void print(T x){
    static int buf[30],cnt;
    if(x==0)print('0');
    else{
        if(x<0)print('-'),x=-x;
        for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
        while(cnt)print((char)buf[cnt--]);
    }
}
inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
vector<int> G[N],D[N]; bool ins[N];
int fi[N],b[M],ne[M],c[M],E,f[N][18],dis[N],fa[N],qq[N];
int n,m,Q,st[N],dfn[N],low[N],ind,top,cnt,dep[N],ab[N],cir[N];
void add(int x,int y,int z){
    ne[++E]=fi[x],fi[x]=E,b[E]=y,c[E]=z;
    ne[++E]=fi[y],fi[y]=E,b[E]=x,c[E]=z;
}
void dfs(int u,int pre){
    f[u][0]=pre;
    for(int i=fi[u];i;i=ne[i])
    if(b[i]!=pre){
        int v=b[i];
        dis[v]=dis[u]+c[i];
        dep[v]=dep[u]+1,dfs(v,u);
    }
}
void work(int u,int s,int d){
    qq[u]=0,cir[++cnt]=d;
    for(int i=u;i!=s;i=fa[i])
    qq[fa[i]]=qq[i]+ab[i],cir[cnt]+=ab[i];
    for(int i=u;i!=s;i=fa[i])
    add(cnt,i,min(qq[s]-qq[i],qq[i]+d));
    add(cnt,s,0);
}
void tarjan(int u,int pre){
    dfn[u]=low[u]=++ind;
    ins[u]=1,fa[u]=pre;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i],L=D[u][i];
        if(!dfn[v])ab[v]=L,tarjan(v,u),
        low[u]=min(low[u],low[v]);
        else if(v!=pre)low[u]=min(low[u],dfn[v]);
        if(low[v]>dfn[u])add(u,v,L);
        if(fa[v]!=u&&low[v]>=dfn[u])work(v,u,L);
    }
}
void bz(){
    for(int j=1;j<=16;j++)
    for(int i=1;i<=cnt;i++)
    f[i][j]=f[f[i][j-1]][j-1];
}
int lca(int x,int y){
    if(dep[x]<dep[y])swap(x,y);
    int k=dep[x]-dep[y];
    for(int i=0;i<=16;i++)
    if(k&(1<<i))x=f[x][i];
    if(x==y)return x;
    for(int i=16;~i;i--)
    if(f[x][i]!=f[y][i])
    x=f[x][i],y=f[y][i];
    return (x==y)?x:f[x][0];
}
int findc(int x,int rt){
    int k=dep[x]-dep[rt]-1;
    for(int i=0;i<=16;i++)
    if(k&(1<<i))x=f[x][i];
    return x;
}
int getdis(int x,int y){
    int rt=lca(x,y);
    if(rt<=n)return dis[x]+dis[y]-2*dis[rt];
    int tx=findc(x,rt),ty=findc(y,rt);
    int len=dis[x]-dis[tx]+dis[y]-dis[ty];
    int L=abs(qq[tx]-qq[ty]);
    return len+min(L,cir[rt]-L);
}
int main(){
    read(n),read(m),read(Q),cnt=n;
    for(int i=1,x,y,z;i<=m;i++)
    read(x),read(y),read(z),
    G[x].push_back(y),G[y].push_back(x),
    D[x].push_back(z),D[y].push_back(z);
    E=0,tarjan(1,0),dfs(1,0),bz();
    while(Q--){
        int x,y;
        read(x),read(y);
        printf("%d\n",getdis(x,y));
    }
}
posted @ 2018-12-03 11:17  Romeolong  阅读(...)  评论(...编辑  收藏