#include <bits/stdc++.h> using namespace std; const int maxn = 1e5+10; #define push_back pb #define make_pair mp #define first X #define second Y typedef long long ll; int n, m; struct Edge{ int to, next; ll w; int id; }edge[maxn<<1]; ll w[maxn]; int tot, head[maxn]; int F[maxn*2]; int p[maxn]; int cnt; int rmq[maxn*2]; ll dis[maxn<<1]; int dfs_clock; int l[maxn<<1], r[maxn<<1]; int G[maxn]; struct ST{ int mm[maxn<<1]; int dp[maxn<<1][26]; void init(int n){ mm[0] = -1; for(int i=1; i<=n; i++){ mm[i] = ((i)&(i-1))==0?mm[i-1]+1:mm[i-1]; dp[i][0] = i; } for(int j=1; j<=mm[n]; j++){ for(int i=1; i+(1<<j)-1<=n; i++){ dp[i][j] = rmq[dp[i][j-1]]<rmq[dp[i+(1<<(j-1))][j-1]]? dp[i][j-1]:dp[i+(1<<(j-1))][j-1]; } } } int query(int a, int b){ if(a>b) swap(a, b); int k = mm[b-a+1]; return rmq[dp[a][k]]<=rmq[dp[b-(1<<k)+1][k]]? dp[a][k]:dp[b-(1<<k)+1][k]; } }st; void init(){ tot = 0; memset(head, -1, sizeof(head)); dfs_clock = 0; } void add_edge(int u, int v, int w, int id){ edge[tot].to = v; edge[tot].w = w; edge[tot].id = id; edge[tot].next = head[u]; head[u] = tot++; } void dfs(int u, int pre, int dep){ F[++cnt] = u; rmq[cnt] = dep; p[u] = cnt; l[u] = ++dfs_clock; for(int i=head[u]; ~i; i=edge[i].next){ int v = edge[i].to; if(v == pre) continue; dis[v] = dis[u]+edge[i].w; G[edge[i].id] = v; dfs(v, u, dep+1); F[++cnt] = u; rmq[cnt] = dep; } r[u] = dfs_clock; } void lca_init(int rt, int n){ cnt = 0; dfs(rt, rt, 0); st.init(2*n); } int lca_query(int u, int v){ return F[st.query(p[u], p[v])]; } ll c[maxn<<2]; int lowbit(int x){ return x&(-x); } void update(int x, int val){ while(x<=n){ c[x] += val; x += lowbit(x); } } ll sum(int x){ ll ans = 0; while(x>0){ ans += c[x]; x -= lowbit(x); } return ans; } ll cal(int x,int y) { return sum(l[x])+sum(l[y])-2*sum(l[lca_query(x,y)]); } int main(){ int _; scanf("%d", &_); while(_--){ scanf("%d%d", &n, &m); memset(dis, 0, sizeof(dis)); memset(c, 0, sizeof(c)); init(); int u, v; int ww; for(int i=1; i<=n-1; i++){ scanf("%d%d%d", &u, &v, &ww); add_edge(u, v, ww, i); add_edge(v, u, ww, i); w[i] = ww; } int X,Y; int Z; scanf("%d%d%lld",&X,&Y,&Z); w[n] = Z; int rt = 1; lca_init(rt, n); for(int i=1; i<n; i++){ update(l[G[i]], w[i]); update(r[G[i]]+1, -w[i]); } ll ans=0; int op; ll vv; while(m--) { scanf("%d%d%lld", &op, &u, &vv); if(op == 0){ if(u==n) w[n] = vv; else{ update(l[G[u]],vv-w[u]); update(r[G[u]]+1,-vv+w[u]); w[u]=vv; } } else { ans=cal(u,vv); ans=min(ans,cal(u,X)+cal(vv,X)); ans=min(ans,cal(u,Y)+cal(vv,Y)); ans=min(ans,cal(u,X)+cal(vv,Y)+Z); ans=min(ans,cal(u,Y)+cal(vv,X)+Z); printf("%lld\n",ans); } } } return 0; }
|