# [bzoj2125]最短路——仙人掌,圆方树

### 思路

/*=======================================
* Author : ylsoi
* Time : 2019.3.3
* Problem : luogu5263
* E-mail : ylsoi@foxmail.com
* ====================================*/
#include<bits/stdc++.h>

#define REP(i,a,b) for(int i=a,i##_end_=b;i<=i##_end_;++i)
#define DREP(i,a,b) for(int i=a,i##_end_=b;i>=i##_end_;--i)
#define debug(x) cout<<#x<<"="<<x<<" "
#define fi first
#define se second
#define mk make_pair
#define pb push_back
typedef long long ll;

using namespace std;

void File(){
freopen("luogu5263.in","r",stdin);
freopen("luogu5263.out","w",stdout);
}

_=0; T f=1; char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())_=(_<<1)+(_<<3)+(c^'0');
_*=f;
}

const int maxn=10000+10;
const int maxm=20000+10;
int n,m,q;
int beg[maxn],to[maxm<<1],las[maxm<<1],w[maxm<<1],cnte=1;
int dfn[maxn],low[maxn],cnt_dfn,pre[maxn];
int tot,anc[maxn<<1][21],dep[maxn<<1],Log[maxn<<1];
ll val[maxn<<1][21],ss[maxn<<1],ssum[maxn<<1];
vector<int>G[maxn<<1];

las[++cnte]=beg[u],beg[u]=cnte,to[cnte]=v,w[cnte]=ww;
las[++cnte]=beg[v],beg[v]=cnte,to[cnte]=u,w[cnte]=ww;
}

void tarjan(int u,int fe){
dfn[u]=low[u]=++cnt_dfn;
for(int i=beg[u];i;i=las[i]){
if((i^1)==fe)continue;
int v=to[i];
if(!dfn[v]){
pre[v]=i;
tarjan(v,i);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])G[u].pb(v),val[v][0]=w[i];
}
else low[u]=min(low[u],dfn[v]);
}
for(int i=beg[u];i;i=las[i]){
if((i^1)==fe)continue;
int v=to[i];
if(pre[v]!=i && dfn[v]>dfn[u]){
ll sum=w[i],s=w[i];
for(int p=v;p!=u;p=to[pre[p]^1])
sum+=w[pre[p]];
++tot;
G[u].pb(tot);
for(int p=v;p!=u;p=to[pre[p]^1]){
G[tot].pb(p);
ssum[p]=sum,ss[p]=s;
val[p][0]=min(s,sum-s),s+=w[pre[p]];
}
}
}
}

void init(){
int u,v,ww;
REP(i,1,m){
}
REP(i,2,n<<1)Log[i]=Log[i>>1]+1;
}

void dfs(int u,int fh){
anc[u][0]=fh,dep[u]=dep[fh]+1;
REP(i,0,G[u].size()-1){
int v=G[u][i];
dfs(v,u);
}
}

ll solve(int x,int y){
if(dep[x]<dep[y])swap(x,y);
ll ret=0;
for(int d=Log[dep[x]-dep[y]];dep[x]!=dep[y];--d){
if(dep[anc[x][d]]>=dep[y])ret+=val[x][d],x=anc[x][d];
}
if(x==y)return ret;
for(int d=Log[dep[x]];d>=0;--d)
if(anc[x][d]!=anc[y][d]){
ret+=val[x][d],x=anc[x][d];
ret+=val[y][d],y=anc[y][d];
}
if(anc[x][0]>n){
return ret+min(abs(ss[x]-ss[y]),ssum[x]-abs(ss[x]-ss[y]));
}
else{
return ret+val[x][0]+val[y][0];
}
}

void work(){
tot=n;
tarjan(1,0);
dfs(1,0);
REP(j,1,15)REP(i,1,tot)
if((1<<j)<=dep[i]-1){
anc[i][j]=anc[anc[i][j-1]][j-1];
val[i][j]=val[i][j-1]+val[anc[i][j-1]][j-1];
}
int u,v;
REP(i,1,q){