HDU-6393 Traffic Network in Numazu (lca+rmq+ tree-like array)

Source: Internet
Author: User

This problem is equivalent to combining the two questions:

http://poj.org/problem?id=2763

Http://codeforces.com/gym/101808/problem/K

Test instructions: There are n each point n edge of the weighted graph (equivalent to a tree more than one edge), two operations: Modify the weight of an edge; find the shortest path between two points.

Analysis: Take any one edge out, the remaining n-1 can be combined with LCA to solve the shortest path. When asked, compare the size of the path by removing the Bien Hoa only through the edges of the tree, the minimum is the shortest path of two points.

The tree array differential maintains the distance from the root node, and records the range of maintenance required according to the DFS sequence.

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;#defineMAXN 100005typedefLong LongLL;structedge{intTo,next,id;} EDGE[MAXN<<1]; intn,a[maxn],head[maxn],dep[maxn<<1],cnt,pos[maxn],dfs_seq[maxn<<1],dfn,f[maxn<<1][ -];intL[MAXN],R[MAXN],DFS_CLOCK,G[MAXN]; LL W[maxn],c[maxn];inlinevoidAddintUintVintID) {edge[cnt].to=v; Edge[cnt].next=Head[u]; Edge[cnt].id=ID; Head[u]=cnt++;} InlineintLowbit (intx) {return(x) & (-x);} voidinit () {memset (head,-1,sizeof(head)); memset (POS,-1,sizeof(POS)); memset (C,0,sizeof(C)); CNT=dfn=0; Dfs_clock=0;} voidDfsintUintDeep ) {DFS_SEQ[DFN]=u,dep[dfn]=deep,pos[u]=dfn++; L[u]=++Dfs_clock;  for(intI=head[u];~i;i=Edge[i].next) {        intv=edge[i].to; if(pos[v]==-1) {G[edge[i].id]=v;//ImportantDFS (v,deep+1); DFS_SEQ[DFN]=u,dep[dfn++]=Deep ; }} R[u]=Dfs_clock;} voidINIT_RMQ (intN) {     for(intI=1; i<=n;++i) f[i][0]=i;  for(intj=1;(1&LT;&LT;J) <=n;++j) for(intI=1; i+ (1&LT;&LT;J)-1<=n;++i) {            if(dep[f[i][j-1]]<dep[f[i+ (1<< (J-1))][j-1]]) f[i][j]=f[i][j-1]; Elsef[i][j]=f[i+ (1<< (J-1))][j-1]; }} inlineintRMQ (intLintR) {    intk=0;  while(1<< (k +1) <=r-l+1) ++K; if(Dep[f[l][k]]<dep[f[r-(1&LT;&LT;K) +1][K]])returnF[l][k]; returnf[r-(1&LT;&LT;K) +1][k];} InlineintLcaintUintv) {    if(Pos[u]>pos[v])returnDFS_SEQ[RMQ (Pos[v],pos[u]); returnDFS_SEQ[RMQ (Pos[u],pos[v]);} InlinevoidUpdateinti,ll x) {     for(; I<=n;i+=lowbit (i)) c[i]+=x;} inline LL sum (inti) {LL s=0;  for(;i>0; I-=lowbit (i)) s+=C[i]; returns;} Inline LL Dist (intUintv) {    returnSUM (L[u]) +sum (L[v])-2*sum (L[LCA (U,V)]);} intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); Freopen ("OUT.txt","W", stdout); #endif    inti,u,v,k,q,s,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); INIT_RMQ (DFN-1); intx, y;        LL Z; scanf ("%d%d%lld", &x,&y,&z);//section n EdgeW[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,x)); Ans=min (Ans,dist (u,y) +Dist (v,y)); 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;}

HDU-6393 Traffic Network in Numazu (lca+rmq+ tree-like array)

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.