Hangzhou Electric Multi-school seventh field traffic Network in Numazu

Source: Internet
Author: User

Problem 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

Analysis

When the game is silly, change the tree to a base ring tree.

    • Remove any edge of the ring first, and two points of the side connection are recorded as R1,r2
    • For each query, the distance between x and Y is three: (1) The shortest distance from the tree after the edge is removed, and (2) x->r1->r2->y; (3) x->r2->r1->y. Take a minimum of three values to
Code
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < queue> #include <map> #include <cassert> #include <cmath> #include <vector>using namespace Std;typedef Long Long ll;const int maxn=100050;int fa[maxn];int Find (int x) {return x==fa[x]?x: (Fa[x]=find (fa[x]));} struct Edge {int v,nxt,d;}    E[maxn*2];int h[maxn],tot,n,q;void addedge (int x,int y,int d) {e[++tot]= (Edge) {y,h[x],d}; H[x]=tot;}    ll sum[maxn];void Add (int x,int val) {assert (x>0);        while (x<=n) {sum[x]+=val;    x+= (x&-x);    }}LL query (int x) {ll ans=0;        while (x) {ans+=sum[x];    x-= (x&-x); } return ans;    int f[maxn][20],idx[maxn],cnt,dep[maxn],r[maxn];void dfs (int x,int par,int val) {fa[x]=f[x][0]=par;    dep[x]=dep[par]+1;    idx[x]=++cnt;    Add (Cnt,val);    for (int i = 1; f[x][i-1]; ++i) f[x][i]=f[f[x][i-1]][i-1]; for (int i = h[x]; i; i=e[i].nxt) {if (PAR!=E[I].V) DFS (E[I].V,X,E[I].D);    } r[x]=cnt; Add (cnt+1,-val);}    int LCA (int x,int y) {if (Dep[x]<dep[y]) swap (x, y);    int h=dep[x]-dep[y];    for (int i = n; i >= 0; i) {if (h& (1<<i)) x=f[x][i];    } if (x==y) return x;    for (int i = n; i >= 0; i.) {if (F[x][i]!=f[y][i]) x=f[x][i],y=f[y][i]; } return f[x][0];}    ll dis (int x,int y) {int Z=lca (x, y); return query (Idx[x]) +query (Idx[y])-query (idx[z]) * *;}    int X[maxn],y[maxn],w[maxn];int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&q);        for (int i = 0; I <= N; ++i) h[i]=0,fa[i]=i,sum[i]=0;        cnt=0,tot=0;        int r1=0,r2=0,bw=0,id;            for (int i = 1; I <= n; ++i) {int x,y,w;            scanf ("%d%d%d", &x,&y,&w);            if (find (x) ==find (y)) {r1=x,r2=y,bw=w,id=i;            } else Addedge (x,y,w), Addedge (y,x,w), Fa[find (x)]=find (y);        X[i]=x,y[i]=y,w[i]=w;     }   DFS (1,0,0);            while (q--) {int op,x,y;            scanf ("%d%d%d", &op,&x,&y);                if (op==0) {if (id==x) bw=y; else {int u= (fa[x[x]]==y[x]?                    X[X]:Y[X]);                    Add (idx[u],-w[x]+y);                    Add (r[u]+1,w[x]-y);                W[x]=y;                }} else {ll ans=dis (x, y);                Ans=min (Ans,dis (r1,x) +dis (r2,y) +BW);                Ans=min (Ans,dis (r1,y) +dis (r2,x) +BW);            printf ("%lld\n", ans); }}} return 0;}

Hangzhou Electric Multi-school seventh field traffic Network in Numazu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.