Description
|
Once again, James Bond is on his way to saving the world. Bond's latest mission requires him to travel between several pairs of cities in a certain country.
The country has N cities (numbered by 1, 2, . . ., N), connected by M bidirectional roads. Bond is going to steal a vehicle, and drive along the roads from city s to city t. The country's police will be patrolling the roads, looking for Bond, however, not all roads get the same degree of attention from the police.
More formally, for each road MI6 has estimated its dangerousness, the higher it is, the more likely Bond is going to be caught while driving on this road. Dangerousness of a path from s to t is defined as the maximum dangerousness of any road on this path.
Now, it's your job to help Bond succeed in saving the world by finding the least dangerous paths for his mission.
Input
There will be at most 5 cases in the input file.
The first line of each case contains two integers N, M (2 ≤ N≤ 50000, 1≤ M ≤ 100000) – number of cities and roads. The next M lines describe the roads. The i-th of these lines contains three integers: xi, yi, di (1 ≤ xi, yi ≤ N, 0 ≤ di ≤ 109) - the numbers of the cities connected by the ith road and its dangerousness.
Description of the roads is followed by a line containing an integer Q (1 ≤ Q ≤ 50000), followed by Q lines, the i-th of which contains two integers si andti (1 ≤ si, ti ≤ N, si != ti).
Consecutive input sets are separated by a blank line.
Output
For each case, output Q lines, the i-th of which contains the minimum dangerousness of a path between cities si and ti. Consecutive output blocks are separated by a blank line.
The input file will be such that there will always be at least one valid path.
Sample Input |
Output for Sample Input |
4 5 1 2 10 1 3 20 1 4 100 2 4 30 3 4 10 2 1 4 4 1
2 1 1 2 100 1 1 2 |
20 20
100 |
ProblemSetter: Ivan Krasilnikov
思路:最小瓶颈路。堆+prim+LCA
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #define ll(x) (1<<x) #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof f) using namespace std; const int mp=5e4+9; const int me=5e5+9; const int oo=0x3f3f3f3f; class Edge { public: int v,next,u,w; Edge(){} Edge(int _u,int _v,int _w){u=_u;v=_v;w=_w;} bool operator<(const Edge&t)const { return w>t.w; } }; class Graph_tree { public: int head[mp],edge; Edge e[me*2]; int n,MST; void clear() { clr(head,-1);edge=0; } void add(int u,int v,int w) { e[edge].w=w;e[edge].v=v;e[edge].u=u;e[edge].next=head[u];head[u]=edge++; } bool vis[mp];int dis[mp],pre[mp],dep[mp]; void prim() { priority_queue<Edge>Q; FOR(i,0,n)vis[i]=0,dis[i]=oo,pre[i]=-1; vis[1]=1;dep[1]=0; int u,v; for(int i=head[1];~i;i=e[i].next) { v=e[i].v; dis[v]=e[i].w; Q.push(Edge(1,v,e[i].w)); }MST=0; while(!Q.empty()) { Edge ret=Q.top();Q.pop(); u=ret.v; if(vis[u])continue; pre[u]=ret.u; dep[u]=dep[ret.u]+1; vis[u]=1; dis[u]=ret.w;MST+=ret.w; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(!vis[v]&&dis[v]>e[i].w) { dis[v]=e[i].w; Q.push(Edge(u,v,e[i].w)); } } } } int maxcost[mp][32],anc[mp][32]; void init_RMQ() { FOR(i,1,n) { anc[i][0]=pre[i];maxcost[i][0]=dis[i]; for(int j=1;ll(j)<=n;++j)anc[i][j]=-1; } for(int j=1;ll(j)<=n;++j) FOR(i,1,n) if(anc[i][j-1]!=-1) { int fa=anc[i][j-1]; anc[i][j]=anc[fa][j-1]; maxcost[i][j]=max(maxcost[i][j-1],maxcost[fa][j-1]); } } int RMQ(int u,int v) { if(dep[u]<dep[v])swap(u,v); int log; for(log=1;ll(log)<=dep[u];++log);--log; int ans=-1; for(int i=log;i>=0;--i) if(dep[u]-ll(i)>=dep[v]) { ans=max(ans,maxcost[u][i]);u=anc[u][i]; } if(u==v)return ans; for(int i=log;i>=0;--i) if(anc[u][i]!=-1&&anc[u][i]!=anc[v][i]) { ans=max(maxcost[u][i],ans);u=anc[u][i]; ans=max(maxcost[v][i],ans);v=anc[v][i]; } ans=max(ans,dis[u]); ans=max(ans,dis[v]); return ans; } int LCA(int u,int v) { int dis1=-1,dis2=-1; if(dep[u]<dep[v])swap(u,v); while(dep[u]!=dep[v]) { dis1=max(dis1,dis[u]); u=pre[u]; } while(u^v) { dis1=max(dis1,dis[u]); dis2=max(dis[v],dis2); u=pre[u];v=pre[v]; } return max(dis1,dis2); } }sp; int main() { int n,m,a,b,c,cas=0; while(~scanf("%d%d",&n,&m)) { sp.clear();sp.n=n; FOR(i,1,m) { scanf("%d%d%d",&a,&b,&c); sp.add(a,b,c);sp.add(b,a,c); } sp.prim(); int Q; if(cas)printf("\n");++cas; scanf("%d",&Q); sp.init_RMQ(); while(Q--) { scanf("%d%d",&a,&b); printf("%d\n",sp.RMQ(a,b)); } } }