3287 Lorry Transport
2013 Noip National League Improvement Group
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description
Description
A state-owned N-City, numbering from 1 to N, between cities there is m two-way road. Each road has a weight limit for the vehicle, which is referred to as the limit. There are now Q trucks transporting goods, and drivers want to know that each vehicle can carry more than the maximum amount of cargo in the case of a vehicle's limited weight.
Enter a description
Input Description
The first line has two integer n,m separated by a space, representing a state-owned N-City and M-Road.
The next M-line is 3 integers x, y, Z, and every two integers separated by a space, indicating that from the city of X to the city of Y there is a road limited to Z. Note: x is not equal to Y, and there may be multiple roads between the two cities.
The next line has an integer q, which indicates that there is a Q truck that needs shipping.
Next Q line, two integers x, y per line, separated by a space, indicating that a lorry needs to transport goods from the X city to the Y city, note: x is not equal to Y.
Output description
Output Description
The output is a total Q line, an integer per line, indicating the maximum load for each lorry. If the truck cannot reach its destination, output-1.
Sample input
Sample Input
4 3
1 2 4
2 3 3
3 1 1
3
1 3
1 4
1 3
Sample output
Sample Output
3
-1
3
Data range and Tips
Data Size & Hint
For 30% data, 0 < n < 1,000,0 < m < 10,000,0 < Q < 1,000;
For 60% data, 0 < n < 1,000,0 < m < 50,000,0 < Q < 1,000;
For 100% data, 0 < n < 10,000,0 < m < 50,000,0 < Q < 30,000,0≤z≤100,000.
Exercises
Multiply LCA on a tree ...
Code:
1#include <cstdio>2#include <iostream>3#include <vector>4#include <algorithm>5 #defineMAXN 100106 7 using namespacestd;8 9vector<int>P[MAXN],C[MAXN];Ten One structnode A { - intl,r,v; -}s[50010]; the - structSS - { - intp; + intC; -} f[maxn][ -]; + A BOOLCMP (node A,node b) at { - returnA.v>B.V; - } - intDEEP[MAXN],N,M,FA[MAXN],KG[MAXN]; - - voidGetree (intKintd) in { -deep[k]=D; tokg[k]=1; + for(intI=1;(1<<i) <=deep[k];i++) - { thef[k][i].p=f[f[k][i-1].p][i-1].P; *F[k][i].c=min (f[k][i-1].c,f[f[k][i-1].p][i-1].c); $ } Panax Notoginseng for(intI=0; I<p[k].size (); i++) - if(!Deep[p[k][i]]) the { +f[p[k][i]][0].p=K; Af[p[k][i]][0].c=C[k][i]; theGetree (p[k][i],d+1); + } - } $ intLcaintAintb) $ { - if(deep[a]<deep[b]) swap (A, b); - intt=deep[a]-Deep[b]; the intans=0x7fffffff; - for(intI=0; i<= -; i++) Wuyi if((1<<i) &t) { theans=min (f[a][i].c,ans); -A=F[A][I].P; Wu } - for(intI= -; i>=0; i--) About if(f[a][i].p!=F[B][I].P) $ { -ans=min (f[b][i].c,ans); -ans=min (f[a][i].c,ans); -A=F[A][I].P; Ab=F[B][I].P; + } the if(a==b)returnans; - Else returnMin (f[a][0].c,min (f[b][0].c,ans)); $ } the the intFindintx) the { the if(fa[x]==x)returnx; -fa[x]=find (Fa[x]); in returnFa[x]; the } the About voidInit () the { thescanf"%d%d",&n,&m); the for(intI=0; i<m;i++) +scanf"%d%d%d",&s[i].l,&s[i].r,&s[i].v); -Sort (s+0, s+m,cmp); the for(intI=0; i<=n;i++)Bayifa[i]=i; the } the intMain () - { - init (); the for(intI=0; i<m;i++) the { the intpp=find (S[I].L); the intqq=find (S[I].R); - if(pp!=QQ) the { thefa[pp]=QQ; the P[s[i].l].push_back (S[I].R);94 C[s[i].l].push_back (S[I].V); the P[s[i].r].push_back (S[I].L); the C[s[i].r].push_back (S[I].V); the }98 } About for(intI=1; i<=n;i++) - if(!kg[i]) getree (i,1); 101scanf"%d",&m); 102 for(intI=0; i<m;i++)103 {104 intx, y; thescanf"%d%d",&x,&y);106 if(Find (x)!=find (y)) printf ("-1\n");107 Elseprintf"%d\n", LCA (x, y)); 108 } 109 return 0; the}
C + + Road Advanced--lca (trucking)