Traffic Network in Numazu topic description
Chika is elected mayor of Numazu. She needs to manage the traffic. To manage the traffic was too hard for her. So she needs your help.
You were given the map of the City--an undirected connected weighted graph with n nodes and n edges, and you had to fini SH Q missions. Each mission consists of 3 integers OP, X and Y.
When op=0, you need to modify the weight of the Xth edge to Y.
When op=1, you need to calculate the length of the shortest path from node X to node Y.
Input
The first line contains a single integer T, the number of test cases.
Each test case starts with a line containing the integers N and Q, the number of nodes (and edges) and the number of Queri Es. (3≤n≤105) (1≤q≤105)
Each of the following N lines contain the description of the edges. The ith line represents the ith edge, which contains 3 space-separated integers UI, vi, and WI. This means the there is an undirected edge between nodes UI and VI, with a weight of wi. (1≤ui,vi≤n) (1≤wi≤105)
Then Q lines follow, the ith line contains 3 integers OP, X and Y. The meaning has been described above. (0≤op≤1) (1≤x≤105) (1≤y≤105)
It is guaranteed, the graph contains no self loops or multiple edges.
Output
For each test case, and for each mission whose op=1, print one line containing one integer, the length of the shortest pat H between X and Y.
Sample input
2
5 5
1 2 3
2 3 5
2 4 5
2 5 1
4 3 3
0 1 5
1 3 2
1 5 4
0 5 4
1 5 1
5 3
1 2 3
1 3 2
3 4 4
4 5 5
2 5 5
0 1 3
0 4 1
1 1 4
Sample output
5
6
6
6
Base Ring Tree
N points but there are n edges so it must be a ring
Simply put, the tree is on the other side.
Processing and processing of tree-treated and ring
Reference https://www.cnblogs.com/cly-none/p/9314812.html
The first use and check set maintenance, find that ring, the ring in any one of the edges to be removed to become a tree. The tree can be treated with lca+ tree array & tree, and the ring will be directly contracted. Three roads, one is to walk the tree, one to go to X and then to Y to V, one to go to Y and then x to U.
Lca
Find templates for recent public ancestors
//+ DFSvoid dfs(ll u,ll fa){//dfs建树 dep[u]=dep[fa]+1; f[u][0]=fa;//初始化每个点的父节点 L[u]=++dfs_clock; for(int i=head[u];i;i=e[i].next){ int v=e[i].v; if(v!=fa){ G[e[i].id]=v; dfs(v,u); } } R[u]=dfs_clock;}//+ 初始化void rmq_init(int n){ for(int j=1;j<=19;j++) for(int i=1;i<=n;i++) if(f[i][j-1]) f[i][j] = f[f[i][j-1]][j-1];}//+ lca&rmqint lca(int u,int v){ if(dep[u]<dep[v]) swap(u,v);//深度深的先处理 for(int i=19;i>=0;i--){ if(dep[u]>=dep[v]+(1<<i)){ u = f[u][i]; } } if(u==v){//跳到同一深度判断是否完成 return u; } for(int i=19;i>=0;i--){//一起跳 if(f[u][i]!=f[v][i]){ u=f[u][i]; v=f[v][i]; } } return f[u][0];}
Time stamp
Dfs_clock as the name implies is the time to traverse to that point.
R[] Records the point in time at which points are accessed l[] the point at which the node's deepest child nodes are recorded. This processing facilitates the tree-like array.
Tree-like array
Modifications are supported for this interval prefix and modification
Distance
The
prefix and has been maintained so the minimum distance of two points is the prefix of two points and the prefixes of the parent node minus twice times and
#include <bits/stdc++.h>using namespace std; #define MAXN 100005typedef long long ll;struct edge{int v,next,id;} E[maxn<<1];int N,A[MAXN],HEAD[MAXN],DEP[MAXN<<1],CNT,POS[MAXN],DFS_SEQ[MAXN<<1],DFN,F[MAXN <<1][20];int l[maxn],r[maxn],dfs_clock,g[maxn];ll w[maxn],c[maxn];inline void Add (int u,int v,int ID) {cnt++; E[cnt].v=v; E[cnt].next=head[u]; E[cnt].id=id; head[u]=cnt;} inline int lowbit (int x) {return (x) & (-X);} void Init () {memset (head,0,sizeof (head)); memset (c,0,sizeof (C)); memset (dep,0,sizeof (DEP)); cnt=0; dfs_clock=0;} void Dfs (ll u,ll FA) {//dfs build dep[u]=dep[fa]+1; f[u][0]=fa;//initializes the parent node of each point l[u]=++dfs_clock; for (int i=head[u];i;i=e[i].next) {int v=e[i].v; if (V!=FA) {g[e[i].id]=v; DFS (V,U); }} R[u]=dfs_clock;} void Rmq_init (int n) {for (Int. j=1;j<=19;j++) for (int i=1;i<=n;i++) if (f[i][j-1]) f[i][j] = F[f[i ][J-1]][J-1];} int LCA (int u,int v{if (Dep[u]<dep[v]) swap (U,V);//depth-deep first processing for (int i=19;i>=0;i--) {if (dep[u]>=dep[v]+ (1<<i)) { U = f[u][i]; }} if (U==v) {//jumps to the same depth to determine if the return U is complete; } for (int i=19;i>=0;i--) {//Jump together if (F[u][i]!=f[v][i]) {u=f[u][i]; V=f[v][i]; }} return f[u][0];} inline void Update (int i,ll x) {for (; I<=n;i+=lowbit (i)) c[i]+=x;} inline ll sum (int i) {ll s=0; for (; I>0;i-=lowbit (i)) s+=c[i]; return s;} inline ll Dist (int u,int v) {return sum (L[u]) +sum (L[v]) -2*sum (L[LCA (u,v)]);} int main () {int i,u,v,k,q,t; ll W; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&q); Init (); for (i=1;i<=n-1;++i) {scanf ("%d%d%lld", &u,&v,&w); Add (u,v,i); Add (v,u,i); W[i]=w; } dfs (1,0); Rmq_init (n); int x, y; ll Z; scanf ("%d%d%lld", &x,&y,&z); W[n] = Z; FoR (i=1;i<n;++i) {update (l[g[i]],w[i]); Update (r[g[i]]+1,-w[i]); } while (q--) {scanf ("%d", &k); if (k==0) {scanf ("%d%lld", &u,&w); if (u==n) w[n] = W; else{Update (L[g[u]],w-w[u]); Update (R[g[u]]+1,-w+w[u]); W[u]=w; }} else{scanf ("%d%d", &u,&v); ll Ans=dist (U,V); Ans=min (Ans,dist (u,x) +dist (v,y) +z); Ans=min (Ans,dist (u,y) +dist (v,x) +z); printf ("%lld\n", ans); }}} return 0;}
Traffic Network in Numazu