Bzoj 4326:noip2015 Transport Plan (two points + differential +LCA)

Source: Internet
Author: User

NOIP2015 Transport Plan
Description
In the 2044 A.D., humans entered the cosmic era. L State n Planets, there are n−1 two-way waterway, each channel is built between two planets, this n−1 waterway connected to all the planets of L country. Little P is in charge of a logistics company, which has many transport plans, each of which is a logistics spaceship that needs to fly from the UI number planet to Planet VI. Obviously, it takes time for the spacecraft to sail through a fairway, and for Fairway J, the time it takes for any spacecraft to pass through it is TJ, and no interference between any two ships will occur. In order to encourage scientific and technological innovation, L King agreed that small p logistics companies to participate in the country's waterway construction, that is, allow small p to a certain channel to transform adult hole, the spacecraft drove through the wormhole does not consume time. The logistics company of small P before the completion of the worm hole was pre-connected with M transport plans. After the construction of the wormhole, the M transport plan will start at the same time, with all the ships starting together. When the M transport plan is completed, the small P logistics company's phased work is completed. If the small p can freely choose which channel to transform the adult hole, try to find out the small P logistics company to complete the phased work of the shortest time is how much?
Input
The first line consists of two positive integer n,m, representing the number of planets in the L state and the number of transport plans that the small P company has pre-connected, and the number of planets from 1 to N. Next n−1 describes the construction of the waterway, where line I contains three integers ai,bi and ti, indicating that the two-way waterway is built between AI and bi two planets, and the time it takes for any spacecraft to sail through it is ti. The data is guaranteed 1≤ai,bi≤n and 0≤ti≤1000. The next M-line describes the transport plan, where line J contains two positive integers uj and VJ, indicating that the J transport plan is from planet Uj to the planet VJ. Data Assurance 1≤ui,vi≤n
Output
The output file contains only an integer that represents the shortest time the logistics company of small P needs to complete a phased operation.
Sample Input
6 3
1 2 3
1 6 4
3 1 7
4 3 6
3 5 5
3 6
2 5
4 5
Sample Output
11
HINT
Transform the 1th channel into adult hole: The three plan time is: 11,12,11, so it takes 12.
Transform the 2nd channel into adult hole: The three plan time is: 7,15,11, so it takes 15.
Transform the 3rd channel into adult hole: The three plan time is: 4,8,11, so it takes 11.
Transform the 4th channel into adult hole: The three plan time is: 11,15,5, so it takes 15.
Transform the 5th channel into adult hole: The three plan time is: 11,10,6, so it takes 11.
Therefore, the 3rd or 5th channel to transform adult holes can make the completion of the phased work of the shortest time, it takes 11.
Source

Analysis:

Two points is still very obvious, two minutes to spend time, for this tree, no more than this time of the plan can not do, for more than this time of the transport plan, we need to use wormholes to make their time narrow to the allowable range. Obviously, this wormhole must be in the intersection of all time-out planning paths, and the time-out plan will have to be within the allowable range after subtracting the edge of the wormhole from the most time-consuming point. We can take advantage of the difference, for each time-out plan, we are at the start and end of the +1, and at their LCA point with their father's side-2, from the bottom up, if a point current number and = time-out plan total, and make all plans within the allowed time, then the point can be used as wormhole.

Other sites measured 95 points, bzoj space is too small will explode

Code:

 ProgramTransport;type Point=^node; Node=Recordx,v:longint; next:point; End;ConstNum=300000;varA:Array[0.. Num ofPoint ; Q:Array[0.. num*3] ofLongint; H:Array[0.. -,0.. num*3] ofLongint; W:Array[0.. Num1..2] ofLongint; R,c,b,f,d,s,v:Array[0.. Num ofLongint; PL:Array[0.. -] ofLongint;  N,i,m,x,y,t:longint; Len,make,maxn:longint;procedureAdd (x,y,v:longint);varP:point;beginnew (P); p^.x:=y; P^.v:=v; P^.NEXT:=A[X]; a[x]:=p;End;procedureDFS (x,y,deep:longint);varP:point;beginnew (P); p:=A[X]; D[x]:=deep; Inc (LEN); Q[len]:=x; r[x]:=Len;  whileP<>Nil  Do   begin     ifP^.x<>y Then beginS[P^.X]:=S[X]+P^.V; DFS (p^.x,x,deep+1);End;  P:=p^.next; Inc (LEN); q[len]:=x; End;End;procedureDFS1 (x,y,sum:longint);varP:point;beginnew (P); p:=A[x];  whileP<>Nil  Do   begin     ifP^.x<>y Then      beginDFS1 (p^.x,x,sum);ifC[p^.x]>=sum Then ifP^.v>make Thenmake:=p^.v; Inc (C[x],c[p^.x]); End; P:=P^.next; End; Inc (C[x],b[x]);End;procedureWork ;varI,j,x,y:longint;begin   fori:=1  toJe Doh[0, I]:=q[i]; pl[0]:=1;  fori:=1  toTrunc (ln (len)/LN (2)) Dopl[i]:=pl[i-1]*2;  fori:=1  toTrunc (ln (len)/LN (2)) Do     forj:=1  tolen+1-2*i Do      beginx:=h[i-1, j]; y:=h[i-1, j+pl[i-1]]; ifD[x]<d[y] ThenH[i,j]:=xElseh[i,j]:=y; End;End;functionLCA (X,y:longint): Longint;varI,t:longint;beginx:=R[X]; y:=R[y]; ifX>y Then beginT:=x; X:=y; y:=t;End; I:=trunc (LN (y-x+1)/LN (2)); X:=H[I,X]; y:=h[i,y-pl[i]+1]; ifD[x]<d[y] ThenExit (x)Elseexit (y);End;functionCheak (Mid:longint): boolean;varI,maxant,sum:longint;beginFillchar (C,sizeof (c),0); Fillchar (b,sizeof (b),0); maxant:=0; sum:=0;  fori:=1  toM Do  ifV[i]>mid Then   beginInc (C[w[i,1]]); Inc (C[w[i,2]]); Dec (B[f[i]),2); ifV[i]>maxant Thenmaxant:=V[i];   Inc (SUM); End; Make:=0; DFS1 (1,0, sum); ifMake=0  Thenexit (FALSE); ifMaxant-make>mid Thenexit (FALSE); Exit (true);End;proceduresolve;varL,r,ans,mid:longint;beginL:=1; r:=maxn*2;  whileL<=r Do    beginMid:= (l+r)Div 2; ifCheak (mid) Then beginAns:=mid; r:=mid-1;End ElseL:=mid+1; End; Writeln (ans);End;beginreadln (n,m); Fillchar (r,sizeof (R),0);  fori:=1  toN-1  Do   beginreadln (x,y,t); Add (x,y,t);   Add (y,x,t); End; Len:=0; s[1]:=0; Dfs1,0,1);  Work  fori:=1  toM Do   beginreadln (W[i,1],w[i,2]); F[i]:=lca (W[i,1],w[i,2]); V[i]:=s[w[i,1]]+s[w[i,2]]-2*S[f[i]]; ifV[i]>maxn Thenmaxn:=V[i]; End; Solve End.
View Code

Bzoj 4326:noip2015 Transport Plan (two points + differential +LCA)

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.