若一条边的v小于等于u,则贡献为l*w/v,否则贡献为l*w/u
将边按v从小到大排序,将询问按u从小到大排序
用树链剖分维护链上和,val[0]表示第一种情况下的贡献,val[1]表示第二种情况下的贡献
一开始val[0]都是0,val[1]=l*w,
然后每到一个询问(s,t,u),就把所有v小于等于u的边修改掉,val[0]改为l*w/v,val[1]改为0
因为是环套外向树,所以把额外的边(exa,exb,exl,exv)拿走后变成了一棵树。
查询时在下面三种情况中取min:
1.(s,t)链上val[0]的和+(s,t)链上val[1]的和/u
2.(s,exa)链上val[0]的和+(t,exb)链上val[0]的和+((s,exa)链上val[1]的和+(t,exb)链上val[1]的和)/u+exl*exw/min(u,exv)
3.(s,exb)链上val[0]的和+(t,exa)链上val[0]的和+((s,exb)链上val[1]的和+(t,exa)链上val[1]的和)/u+exl*exw/min(u,exv)
时间复杂度$O(n\log n)$
#include<cstdio>
#include<algorithm>
#define N 100010
using namespace std;
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
struct edge{int l,x,v,w,id;}E[N];
struct que{int s,t,u,id;}Q[N];
int n,m,q,i,x,y,z,t,exa,exb,exl,exx,exv,exw,cnt,now,V[N],W[N],g[N<<1],nxt[N<<2],v[N<<2],ed,size[N<<1],d[N<<1],f[N<<1],son[N<<1],loc[N<<1],top[N<<1],dfn;
double ans[N],val[2][N<<3];
inline bool cmpe(edge a,edge b){return a.v<b.v;}
inline bool cmpq(que a,que b){return a.u<b.u;}
int F(int x){return f[x]==x?x:F(f[x]);}
inline void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;}
void dfs1(int x,int pre){
size[x]=1;d[x]=d[f[x]=pre]+1;
int heavy=0,sizemax=0,i;
for(i=g[x];i;i=nxt[i])if(v[i]!=pre){
dfs1(v[i],x),size[x]+=size[v[i]];
if(size[v[i]]>sizemax)sizemax=size[v[i]],heavy=v[i];
}
if(heavy)son[x]=heavy;
}
void dfs2(int x,int pre,int t){
loc[x]=++dfn;top[x]=t;
if(son[x])dfs2(son[x],x,t);
for(int i=g[x];i;i=nxt[i])if(v[i]!=pre&&v[i]!=son[x])dfs2(v[i],x,v[i]);
}
void change(int p,int x,int a,int b,int c,double d){
if(a==b){val[p][x]=d;return;}
int mid=(a+b)>>1;
if(c<=mid)change(p,x<<1,a,mid,c,d);else change(p,x<<1|1,mid+1,b,c,d);
val[p][x]=val[p][x<<1]+val[p][x<<1|1];
}
double ask(int p,int x,int a,int b,int c,int d){
if(c<=a&&b<=d)return val[p][x];
int mid=(a+b)>>1;double t=0;
if(c<=mid)t=ask(p,x<<1,a,mid,c,d);
if(d>mid)t+=ask(p,x<<1|1,mid+1,b,c,d);
return t;
}
inline double query(int p,int x,int y){
double t=0;
while(top[x]!=top[y]){
if(d[top[x]]<d[top[y]])swap(x,y);
t+=ask(p,1,1,dfn,loc[top[x]],loc[x]);
x=f[top[x]];
}
if(d[x]>d[y])swap(x,y);
return t+ask(p,1,1,dfn,loc[x],loc[y]);
}
inline double getans(int x,int y,int u){
double t=query(0,x,y)+query(1,x,y)/(double)u,tmp;
tmp=query(0,x,exa)+query(0,y,exb)+(query(1,x,exa)+query(1,y,exb))/(double)u+(double)exl*(double)exw/(double)(exv<u?exv:u);
if(tmp<t)t=tmp;
tmp=query(0,x,exb)+query(0,y,exa)+(query(1,x,exb)+query(1,y,exa))/(double)u+(double)exl*(double)exw/(double)(exv<u?exv:u);
return tmp<t?tmp:t;
}
int main(){
read(n),read(m),read(q);
for(i=1;i<=n;i++)f[i]=i;
for(i=1;i<=n;i++){
read(x),read(y),read(z),read(t);
if(F(x)==F(y))exa=x,exb=y,exl=z,exx=t;
else{
f[f[x]]=f[y];
E[++cnt].l=z,E[cnt].x=t,E[cnt].id=n+cnt;
add(x,n+cnt),add(n+cnt,x),add(y,n+cnt),add(n+cnt,y);
}
}
for(i=1;i<=m;i++)read(V[i]),read(W[i]);
for(exv=V[exx],exw=W[exx],i=1;i<n;i++)E[i].v=V[E[i].x],E[i].w=W[E[i].x];
for(i=1;i<=q;i++)read(Q[i].s),read(Q[i].t),read(Q[i].u),Q[i].id=i;
sort(E+1,E+n,cmpe),sort(Q+1,Q+q+1,cmpq);
dfs1(1,0),dfs2(1,0,1);
for(i=1;i<n;i++)change(1,1,1,dfn,loc[E[i].id],(double)E[i].l*(double)E[i].w);
for(now=i=1;i<=q;i++){
while(now<n&&E[now].v<=Q[i].u)change(0,1,1,dfn,loc[E[now].id],(double)E[now].l*(double)E[now].w/(double)E[now].v),change(1,1,1,dfn,loc[E[now].id],0),now++;
ans[Q[i].id]=getans(Q[i].s,Q[i].t,Q[i].u);
}
for(i=1;i<=q;i++)printf("%.6f\n",ans[i]);
return 0;
}