最小生成树+LCA+uva11354

18 篇文章 0 订阅
18 篇文章 0 订阅

B

NEXT Generation Contest 4

Time Limit – 8 secs

Bond

 

Once again, James Bond is on his way tosaving the world. Bond's latest mission requires him to travel between severalpairs of cities in a certain country.

 

The country has N cities(numbered by 1, 2, . . ., N),connected by M bidirectionalroads. Bond is going to steal a vehicle, and drive along the roads from city s to city t. The country'spolice will be patrolling the roads, looking for Bond, however, not all roadsget the same degree of attention from the police.

 

More formally, for each road MI6has estimated its dangerousness, the higher it is, the more likely Bond isgoing to be caught while driving on this road. Dangerousness of a path from s to t is defined asthe maximum dangerousness of any road on this path.

 

Now, it's your job to help Bond succeedin saving the world by finding the least dangerous paths for his mission.

 

 

Input

There will be at most 5 cases in theinput file.

The first line of each case contains twointegers N, M (2 ≤ N≤ 50000,1≤ M ≤ 100000)– number of cities and roads. The next M lines describe the roads. The i-thof these lines contains three integers: xi, yi, di (1 ≤ xi, yiN, 0 ≤ di ≤ 109)- the numbers of the cities connected by the ith road and itsdangerousness.

 

Description of the roads is followed bya line containing an integer Q (1 ≤ Q ≤ 50000),followed by Q lines, the i-thof which contains two integers si and ti (1 ≤ si, ti  ≤ N, si != ti).

 

Consecutive input sets are separated bya blank line.

 

Output

For each case, output Q lines, the i-thof which contains the minimum dangerousness of a path between cities si and ti. Consecutiveoutput blocks are separated by a blank line.

 

The input filewill 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

题意:n个城市,m条边相连,每次询问两点之间,最大值最小是多少

思路:最小瓶颈路,肯定是最小生成树上的边。先求出最小生成树,然后dfs吧无根树变成有根树,然后处理处i节点的2^j祖先anc[i][j]和到祖先节点的最大值是多少。然后LCA查询,沿着p,q向上走到LCA,并不度更新最大值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=50010;
const int INF=1000000000;
struct node
{
    int u,v,f;
    bool operator<(const node &a)const
    {
        return f<a.f;
    }
}edge[maxn*2];
int n,m;
int pre[maxn],fa[maxn],dep[maxn],cost[maxn];
int anc[maxn][30],maxcost[maxn][30];
vector<pair<int,int> > g[maxn];

int find(int x)
{
    if(x==pre[x])return x;
    return pre[x]=find(pre[x]);
}
void dfs(int u,int f,int depth)
{
    dep[u]=depth;
    fa[u]=u;
    int len=g[u].size();
    for(int i=0;i<len;i++)
    {
        int v=g[u][i].second,w=g[u][i].first;
        if(v==f)continue;
        dfs(v,u,depth+1);
        fa[v]=u;
        cost[v]=w;
    }
}
void preprocess()
{
    for(int i=1;i<=n;i++)
    {
        anc[i][0]=fa[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)continue;
            int a=anc[i][j-1];
            anc[i][j]=anc[a][j-1];
            maxcost[i][j]=max(maxcost[i][j-1],maxcost[a][j-1]);
        }
    }
}
int LCA(int p,int q)
{
    if(dep[p]<dep[q])swap(p,q);
    int k=0;
    while((1<<(k+1))<=dep[p])k++;
    int ans=-INF;
    for(int i=k;i>=0;i--)
        if(dep[p]-(1<<i)>=dep[q])
            {ans=max(ans,maxcost[p][i]);p=anc[p][i];}
    if(p==q)return ans;
    for(int i=k;i>=0;i--)
        if(anc[p][i]!=-1&&anc[p][i]!=anc[q][i])
        {
            ans=max(ans,maxcost[p][i]);p=anc[p][i];
            ans=max(ans,maxcost[q][i]);q=anc[q][i];
        }
    ans=max(ans,max(cost[p],cost[q]));
    return ans;
}
int main()
{
    bool first=true;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(first)first=false;
        else printf("\n");
        for(int i=1;i<=m;i++)
        {
            int x,y,d;
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].f);
        }
        sort(edge+1,edge+m+1);

        for(int i=0;i<=n;i++)
        {
            g[i].clear();
            pre[i]=i;
        }
        for(int i=1;i<=m;i++)
        {
            int x=find(edge[i].u),y=find(edge[i].v);
            if(x!=y)
            {
                pre[x]=y;
                g[x].push_back(make_pair(edge[i].f,y));
                g[y].push_back(make_pair(edge[i].f,x));
            }
        }
        dfs(1,-1,0);
        preprocess();
        int Q;
        scanf("%d",&Q);
        while(Q--)
        {
            int s,t;
            scanf("%d%d",&s,&t);
            printf("%d\n",LCA(s,t));
        }
    }
    return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值