http://codevs.cn/problem/3287/
Title Description
State-owned cities, numbered from the city, there is a two-way road between cities. Every road has a heavy weight on the vehicle.
Limit, or limit weight. Now there is a lorry transporting the goods, and the drivers want to know that each vehicle is not more than the weight of the vehicle.
Cases, the maximum number of goods can be transported.
Input Format
The first line of the input file has two integers separated by a space, indicating the state-owned city and the road
Lu.
The next line is an integer, separated by a space between each of the two integers, indicating the number city
There is a restricted road to the city. Note: Not equal to, there may be many roads between the two cities.
The next line has an integer indicating that there is a truck that needs shipping.
Next line, two integers per line, separated by a space, indicates that a lorry needs to be from the city
Transport goods to the city, note: Not equal to.
output Format
Outputs a total of rows, one integer per line, indicating what the maximum load is for each vehicle. If the goods
The car cannot reach the destination, output.
Input Sample
4 3
1 2 4
2 3 3
3 1 1
3
1 3
1 4
1 3
Output Sample
3
-1
3
Data Range
Exercises
Can prove that the optimal path must be in the largest spanning tree (forest), so the topic is converted to, ask the forest in the two-point path top-right minimum value, connectivity and check set judgment can be.
A query on a tree can be solved in a doubled way.
The node that represents the point up-step arrival
Benquan minimum value in the path that represents the point up step
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<stack>#defineN 10005#defineM 50005#defineDepth 21using namespacestd;intN,m,q,cnt,tot,ans;intF[n],last[n],dep[n];intanc[n][ A],g[n][ A];structhh{intTo,next,w;} e[m<<1];structhhh{intL,r,w;} line[m<<1];BOOLCMP (HHH a,hhh b) {returnA.w>B.W;}intRead () {intret=0;CharC=GetChar (); while(!isdigit (c)) c=GetChar (); while(IsDigit (c)) {ret= (ret<<1) + (ret<<3) +c-'0'; c=GetChar ();} returnret;}voidAddintAintBintW) {e[++tot].to=b; E[tot].next=Last[a]; E[TOT].W=W; Last[a]=tot;}voidBFsintroot) { intI,j,now; Stack<int>s; S.push (Root);d Ep[root]=1; for(i=0; i<depth;i++) anc[root][i]=Root; while(!S.empty ()) { Now=s.top (); S.pop (); if(now!=root) for(i=1; i<depth;i++) {Anc[now][i]=anc[anc[now][i-1]][i-1]; G[now][i]=min (g[now][i-1],g[anc[now][i-1]][i-1]); } for(i=last[now];i;i=e[i].next)if(!Dep[e[i].to]) {anc[e[i].to][0]=Now ; Dep[e[i].to]=dep[now]+1; g[e[i].to][0]=E[I].W; S.push (e[i].to); } }}voidSwimint& X,inth) { inti; for(i=0; h;i++) { if(h&1) x=Anc[x][i]; H>>=1; }}intGetlca (intXinty) { inti,j; if(dep[x]<Dep[y]) swap (x, y); Swim (X,dep[x]-Dep[y]); if(x==y)returnx; while(true) { for(i=0; anc[x][i]!=anc[y][i];i++); if(!i)returnanc[x][0]; X=anc[x][i-1];y=anc[y][i-1]; }}intQueryintXinty) { inti,ret=9999999, H; H=dep[y]-Dep[x]; for(i=depth;i>=0; i--) if((1<<i) <=h) {h-=(1<<i); RET=min (ret,g[y][i]); Y=Anc[y][i]; } returnret;}intFindintx) {returnf[x]==x?x:f[x]=find (F[x]);}intMain () {intI,j,u,v,w,fx,fy,lca; N=read (); m=read (); for(i=1; i<=m;i++) LINE[I].L=read (), Line[i].r=read (), line[i].w=read (); Sort ( Line+1, line+1+m,cmp); for(i=1; i<=n;i++) f[i]=i; for(i=1; i<=m;i++) {u=line[i].l;v=line[i].r;w=LINE[I].W; FX=find (u); fy=Find (v); if(fx!=FY) {F[FX]=fy;cnt++; Add (u,v,w); add (v,u,w); if(cnt==n-1) Break; } } for(i=1; i<=n;i++) for(j=0; j<depth;j++) G[i][j]=9999999; BFS (1); Q=read (); for(i=1; i<=q;i++) {u=read (); v=read (); if(Find (U)!=find (v)) {Puts ("-1");Continue;} LCA=Getlca (U,V); Ans=min (Query (lca,u), query (LCA,V)); printf ("%d\n", ans); } return 0;}
"Codevs 3287" "NOIP2013" train transport