一个中午啊……
本来打算用仙人掌搞的,后来发现直接基环树就可以了,把多出来的那条边单独记录为((dx,dy,dw)),剩下的树剖
然后最短路径要么直接树上跑,要么经过多出来的边,分别讨论就好了
因为这里的树剖只有单点修改和区间查询,于是可以用树状数组
//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='
';
}
const int N=1e5+55;
struct eg{int v,nx,w,u;}e[N<<1],ee[N];int head[N],tot;
inline void add_edge(R int u,R int v,R int w){e[++tot]={v,head[u],w},head[u]=tot;}
int dfn[N],sz[N],vis[N],fa[N],son[N],top[N],tmp[N],vn[N],c[N],dep[N],dx,dy,dw,tim,n,m,u,v,w,op,x,y,ans;
void dfs1(int u){
vis[u]=sz[u]=1,dep[u]=dep[fa[u]]+1;
go(u)if(v!=fa[u]){
if(vis[v])dx=u,dy=v,dw=e[i].w;
else{
fa[v]=u,dfs1(v),sz[u]+=sz[v],vn[v]=e[i].w;
if(sz[v]>sz[son[u]])son[u]=v;
}
}
}
void dfs2(int u,int t){
top[u]=t,dfn[u]=++tim;if(!son[u])return;
dfs2(son[u],t);go(u)if(v!=fa[u]&&v!=son[u]&&!(u==dx&&v==dy)&&!(u==dy&&v==dx))dfs2(v,v);
}
inline void add(R int x,R int y){for(R int i=x;i<=n;i+=i&-i)c[i]+=y-tmp[x];tmp[x]=y;}
inline int qqq(R int l,R int r){
int res=0;for(;r;r-=r&-r)res+=c[r];
for(--l;l;l-=l&-l)res-=c[l];return res;
}
int query(int u,int v){
int res=0;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
res+=qqq(dfn[top[u]],dfn[u]),u=fa[top[u]];
}if(dep[u]<dep[v])swap(u,v);
if(u!=v)res+=qqq(dfn[son[v]],dfn[u]);return res;
}
int main(){
// freopen("testdata.in","r",stdin);
n=read(),m=read();
fp(i,1,n){
u=read(),v=read(),w=read();
ee[i]={v,0,w,u},add_edge(u,v,w),add_edge(v,u,w);
}dfs1(1),dfs2(1,1);
fp(i,1,n)for(R int j=dfn[i];j<=n;j+=j&-j)c[j]+=vn[i];
fp(i,1,n)tmp[dfn[i]]=vn[i];
while(m--){
op=read(),x=read(),y=read();
if(op==1){
u=ee[x].u,v=ee[x].v;
if((u==dx&&v==dy)||(u==dy&&v==dx))dw=y;
else u=dep[u]>dep[v]?u:v,add(dfn[u],y);
}else{
ans=query(x,y);cmin(ans,query(x,dx)+query(y,dy)+dw);
cmin(ans,query(x,dy)+query(y,dx)+dw);print(ans);
}
}return Ot(),0;
}