uva11354 - Bond

来源:互联网 发布:好玩的网络竞技游戏 编辑:程序博客网 时间:2024/06/05 19:27

B

NEXT Generation Contest 4

Time Limit – 8 secs

Bond

 

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 NM (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: xiyidi (1 ≤ xiyi ≤ 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 and ti (1 ≤ siti  ≤ Nsi != 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 




求最小瓶颈路中的最大值,由于数据范围大,

利用倍增法求最大值。

先求出MST,将无根树转化为有根树,进行LCA的预处理,

在求LCA的过程中顺便将s,t两点到公共祖先的路径上的最大边权更新

这样就可以在线处理多组s,t 。



#include<cstdio>#include<cstring>#include<iostream>#include<vector>#include<algorithm>using namespace std;#define N 50050#define M 100010const int inf=0x3f3f3f3f;struct Edge{    int x,y,w;};struct data{    int y,w;};Edge e[M];int fa[N],dep[N],cost[N],P[N];int anc[N][20],maxcost[N][20];bool vis[N];vector <data> G[N];int n,m,Q;bool cmp(Edge a, Edge b){    return a.w<b.w;}int up(int x){    return fa[x]==x? x: fa[x]=up(fa[x]);}void dfs(int u){    vis[u]=1;    for (int i=0; i<G[u].size(); i++)    {        int v=G[u][i].y,w=G[u][i].w;        if (!vis[v])        {            P[v]=u;            cost[v]=w;            dep[v]=dep[u]+1;            dfs( v );        }    }}void Kruskal(){    for (int i=1; i<=n; i++)        G[i].clear();    for (int i=1; i<=n; i++)        fa[i]=i;    sort(e+1,e+1+m,cmp);    int cnt=0;    for (int i=1; i<=m; i++)    {        int f1=up(e[i].x);        int f2=up(e[i].y);        if (f1!=f2)        {            cnt++;            fa[f1]=f2;            G[e[i].x].push_back( (data){e[i].y,e[i].w} );            G[e[i].y].push_back( (data){e[i].x,e[i].w} );            if (cnt==n-1)  break;        }    }    memset(vis,0,sizeof(vis));    P[1]=-1;    cost[1]=dep[1]=0;    dfs(1);}void init(){    for (int i=1; i<=n; i++)    {        anc[i][0]=P[i];        maxcost[i][0]=cost[i];        for (int j=1; (1<<j)<n; j++)            anc[i][j]=-1;    }    for (int j=1; (1<<j) < n; j++)        for (int i=1; i <= n; i++)        {            if (anc[i][j-1]!=-1)            {                int p=anc[i][j-1];                anc[i][j]=anc[ p ][j-1];                maxcost[i][j]=max( maxcost[i][j-1],maxcost[p][j-1]);            }        }}int query(int u,int v){    if (dep[u]<dep[v]) swap(u,v);    int l=0;    while (1<<(l+1)<=dep[u])        l++;    int ans=-inf;    for (int i=l; i>=0; i--)        if (dep[u]-(1<<i)>=dep[v])        {            ans=max(ans, maxcost[u][i]);            u=anc[u][i];        }    if (u==v)  return ans;    for (int i=l; 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,cost[u]);    ans=max(ans,cost[v]);    return ans;}int main(){    int flag=0;    while (scanf("%d%d",&n,&m)!=EOF)    {        if (flag++) printf("\n");        for (int i=1; i<=m; i++)            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);        Kruskal();        init();        scanf("%d",&Q);        for (int i=1; i<=Q; i++)        {            int u,v;            scanf("%d%d",&u,&v);            printf("%d\n",query(u,v));        }    }    return 0;}


原创粉丝点击