Title 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.
Input/output format
Input format:
The input file name is truck.in.
The first line of the input file has two integer n,m separated by a space, indicating a state-owned N-City and M-Road
Lu. 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. Meaning: x is not equal to Y, there may be many 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 format:
The output file name is Truck.out.
The output is a total Q line, an integer per line, indicating the maximum load for each lorry. If the goods
The car cannot reach its destination, output-1.
Input and Output Sample input example # #:
4 31 2 42 3 33 1 131 31 41 3
Sample # # of output:
3-13
Description
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.
Code
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6 #defineLEN 1000057 #defineLog_len 258 #definell Long Long9 #defineINF 0x3f3f3f3fTen using namespacestd; One A intn,m; - intFa[len],parent[log_len][len],dep[len]; - ll Dis[log_len][len]; the - structPa{ll num,d;};//pair -PA Make_pa (ll num,ll d) {PA Tmp;tmp.num=num;tmp.d=d;returntmp;} - structedge{int from, To;ll Val;} E[len]; + BOOLCMP (Edge A,edge b) {returnA.val>b.val;}//Maximum Value First - + //Bingchaji, no rank version A voidInit_bingchaji () { for(intI=0; i<len;i++) fa[i]=i;} at intFind (intA) {if(fa[a]==a)returnAElse returnfa[a]=Find (Fa[a]);} - voidUnite (intAintb) {intPa=find (a), pb=find (b);if(PA!=PB) fa[pa]=PB;} - -Vector<pa>Map[len]; - - voidKruskal () {//Maximum Spanning Tree inSort (e,e+m,cmp); - Init_bingchaji (); to intCnt=0; + for(intI=0; i<m;i++){ - intA=e[i]. from, B=e[i].to;ll v=E[i].val; the if(Find (a) ==find (b))Continue; * Unite (A, b); $ Map[a].push_back (Make_pa (b,v));Panax Notoginseng Map[b].push_back (Make_pa (a,v)); -cnt++; the if(cnt==n-1)return; + } A } the + voidDfsintVintFintd) { -parent[0][v]=F; $dis[0][v]=INF; $dep[v]=D; - for(intI=0; I<map[v].size (); i++){ -PA now=Map[v][i]; the if(now.num!=f) DFS (now.num,v,d+1); - Elsedis[0][v]=now.d;Wuyi } the } - Wu voidInit_lca () { -memset (Dis,inf,sizeof(DIS)); Aboutmemset (dep,-1,sizeof(DEP)); $ for(intI=1; i<=n;i++)if(dep[i]<0) DFS (i,-1,0); - for(intk=0; k +1<log_len;k++){ - for(intI=1; i<=n;i++){ - if(parent[k][i]<0) parent[k+1][i]=-1, dis[k+1][i] =INF; A Else{ +parent[k+1][i]=parent[k][Parent[k][i]]; thedis[k+1][i]=min (dis[k][i],dis[k][parent[k][i]); - } $ } the } the } the theLL LCA (intUintv) { - if(dep[v]>Dep[u]) swap (U,V); inll res=INF; the for(intI=0; i<log_len;i++){ the if(((dep[u]-dep[v)) >> i) &1 ){ Aboutres=min (res,dis[i][u]); theu=Parent[i][u]; the } the } + if(U==V)returnRes; - the for(inti=log_len-1; i>=0; i--) {//not here.-1 will WABayi if(parent[i][u]!=Parent[i][v]) { theres=min (res,dis[i][u]); theres=min (res,dis[i][v]); -u=parent[i][u];v=Parent[i][v]; - } the } theRes=min (res,dis[0][u]); theRes=min (res,dis[0][v]); the returnRes; - } the the intMain () { the //freopen ("01.in", "R", stdin);94 //freopen ("01.out", "w", stdout); the thescanf"%d%d",&n,&m); the for(intI=0; i<m;i++) scanf ("%d%d%lld", &e[i]. from,&e[i].to,&e[i].val);98 About Kruskal (); - Init_lca ();101 102 intKase=0;103scanf"%d",&Kase);104 while(kase--){ the intu,v;106scanf"%d%d",&u,&v);107 if(Find (U) ==find (v)) printf ("%lld\n", LCA (V,u));//find out if the same group108 ElsePuts"-1");109 } the return 0;111}
Seems to use a long long
Note Line 80
And there's my ZZ, line 24.
void Unite (int a,int b) {int pa=find (a), Pb=find (b), if (PA!=PB) fa[pa]=PB;}
Written Pa==pb,orz ...
It took so long to copy a code ...
Rokua P1967 Truck Transport Label: Multiply LCA && minimum bottleneck Road