Description
A has n cities in China, ranging from 1 to n. There are m two-way roads between cities. Each road imposes weight limitations on vehicles. Now there are Q trucks carrying goods. drivers want to know that each truck can carry a maximum of multiple goods without exceeding the vehicle weight limit.
Input Format
The first line has two integers n, m separated by a space, indicating N cities and M roads in.
Next, line M contains three integers x, y, and z in each row. Each integer is separated by a space, which indicates that there is a road with a limited weight of Z from city X to city y.
Note: X is not equal to Y. There may be multiple roads between two cities.
The next line contains an integer Q, indicating that a truck needs to deliver the goods.
Next, in row Q, two integers x and y are separated by a space, indicating that a freight car needs to transport goods from city X to city Y. Note: X is not equal to y.
Output Format
The output contains a total of Q rows. Each line has an integer representing the maximum load of each truck. If the freight car cannot reach the destination, output-1.
[Question]
First, build a tree using the largest Spanning Tree of the graph, and then the problem is converted to the minimum edge weight from two points to the LCA path, which is multiplied.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<ctime> 6 #include<cmath> 7 #include<algorithm> 8 using namespace std; 9 #define INF 100000000010 #define MAXM 5001011 #define MAXN 1001012 struct node{int x,y,v;}E[MAXM];13 struct node2{int y,next,v;}e[MAXN*2];14 int n,m,q,len,vis[MAXN],f[MAXN],Link[MAXN],deep[MAXN],anc[MAXN][25],w[MAXN][25];15 inline int read()16 {17 int x=0,f=1; char ch=getchar();18 while(!isdigit(ch)) {if(ch==‘-‘) f=-1; ch=getchar();}19 while(isdigit(ch)) {x=x*10+ch-‘0‘; ch=getchar();}20 return x*f;21 }22 bool cmp(node a,node b) {return a.v>b.v;} 23 int find(int x) {return f[x]==x?x:f[x]=find(f[x]);}24 void insert(int x,int y,int v) {e[++len].next=Link[x];Link[x]=len;e[len].y=y;e[len].v=v;}25 void dfs(int x)26 {27 vis[x]=1;28 for(int i=1;i<=20;i++) {anc[x][i]=anc[anc[x][i-1]][i-1]; w[x][i]=min(w[x][i-1],w[anc[x][i-1]][i-1]);}29 for(int i=Link[x];i;i=e[i].next)30 if(!vis[e[i].y])31 {32 deep[e[i].y]=deep[x]+1;33 anc[e[i].y][0]=x;34 w[e[i].y][0]=e[i].v;35 dfs(e[i].y);36 }37 }38 int lca(int x,int y)39 {40 if(deep[x]<deep[y]) swap(x,y);41 for(int i=20;i>=0;i--) if(deep[anc[x][i]]>=deep[y]) x=anc[x][i];42 if(x==y) return x;43 for(int i=20;i>=0;i--) if(anc[x][i]!=anc[y][i]) x=anc[x][i],y=anc[y][i];44 return anc[x][0];45 }46 int ask(int x,int f)47 {48 int mn=INF;49 int t=deep[x]-deep[f];50 for(int i=0;i<=16;i++)if(t&(1<<i)){mn=min(mn,w[x][i]);x=anc[x][i];}51 return mn;52 }53 int main()54 {55 freopen("truck.in","r",stdin);56 freopen("truck.out","w",stdout);57 memset(w,127/3,sizeof(w));58 n=read(); m=read();59 for(int i=1;i<=m;i++) {E[i].x=read(); E[i].y=read(); E[i].v=read();}60 sort(E+1,E+m+1,cmp);61 for(int i=1;i<=n;i++) f[i]=i;62 for(int i=1;i<=m;i++)63 {64 int x=find(E[i].x),y=find(E[i].y);65 if(x!=y)66 {67 f[x]=y;68 insert(E[i].x,E[i].y,E[i].v);69 insert(E[i].y,E[i].x,E[i].v);70 }71 }72 for(int i=1;i<=n;i++) if(!vis[i]) dfs(i);73 q=read();74 for(int i=1;i<=q;i++)75 {76 int x=read(),y=read();77 if(find(x)!=find(y)) {printf("-1\n"); continue;}78 int t=lca(x,y);79 printf("%d\n",min(ask(x,t),ask(y,t)));80 }81 return 0;82 }
[Noip2013] freight car transportation