zoukankan      html  css  js  c++  java
  • UVA 11354

    -----------------------

    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 


    -----------------------

    最小生成树上,两点间的最长边就是瓶颈路。

    用LCA快速求出树上两点的瓶颈路

    -----------------------

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    const int maxn=55000;
    const int maxm=250000;
    const int INF=0x3f3f3f3f;
    typedef long long LL;
    
    struct HeapNode{
        int d,u;
        HeapNode(){}
        HeapNode(int a,int b):d(a),u(b){}
        bool operator<(const HeapNode& rhs) const{
            return d>rhs.d;
        }
    };
    struct EdgeNode{
        int to;
        int w;
        int next;
    };
    struct Prim{
        EdgeNode edges[maxm];
        int head[maxn];
        int edge,n;
        void init(int n){
            this->n=n;
            memset(head,-1,sizeof(head));
            edge=0;
        }
        void addedge(int u,int v,int c){
            edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
        }
        bool done[maxn];
        int dis[maxn];
        int pre[maxn];
        int dep[maxn];
        void prim(int s){
            priority_queue<HeapNode>que;
            for (int i=0;i<=n;i++) dis[i]=INF;
            dis[s]=0;
            memset(done,0,sizeof(done));
            memset(dep,0,sizeof(dep));
            que.push(HeapNode(0,s));
            while (!que.empty()){
                HeapNode x=que.top();
                que.pop();
                int u=x.u;
                if (done[u]) continue;
                done[u]=true;
                for (int i=head[u];i!=-1;i=edges[i].next){
                    int v=edges[i].to;
                    int w=edges[i].w;
                    if (!done[v]&&dis[v]>w){
                        dis[v]=w;
                        pre[v]=u;
                        dep[v]=dep[u]+1;
                        que.push(HeapNode(dis[v],v));
                    }
                }
            }
        }
    }solver;
    int n,m;
    int fa[maxn],cost[maxn],L[maxn];
    int anc[maxn][20];
    int maxCost[maxn][20];
    
    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){
                    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 query(int p,int q){
        int log;
        if (L[p]<L[q]) swap(p,q);
        for (log=1;(1<<log)<=L[p];log++);log--;
        int ans=-INF;
        for (int i=log;i>=0;i--){
            if (L[p]-(1<<i)>=L[q]){
                ans=max(ans,maxCost[p][i]);
                p=anc[p][i];
            }
        }
        if (p==q) return ans;
        for (int i=log;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,cost[p]);
        ans=max(ans,cost[q]);
        return ans;
    }
    
    int main()
    {
        int cas=0;
        while (~scanf("%d%d",&n,&m)){
            if (cas!=0) puts("");
            cas++;
            solver.init(n);
            for (int i=0;i<m;i++){
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                solver.addedge(x,y,z);
                solver.addedge(y,x,z);
            }
            solver.prim(1);
            for (int i=1;i<=n;i++){
                fa[i]=solver.pre[i];
                cost[i]=solver.dis[i];
                L[i]=solver.dep[i];
            }
            //for (int i=1;i<=n;i++) cerr<<fa[i]<<" ";cerr<<endl;
            preprocess();
            int Q;
            scanf("%d",&Q);
            while (Q--){
                int x,y;
                scanf("%d%d",&x,&y);
                printf("%d
    ",query(x,y));
            }
        }
        return 0;
    }
    


    -----------------------

    -----------------------

  • 相关阅读:
    将Ajax 中数组转换成字符串 封装成类
    网页中删除数据弹出提示框
    pdo连接数据库
    pdo 的配置与启用
    php中常用的运算符
    [JZOJ 5912] [NOIP2018模拟10.18] VanUSee 解题报告 (KMP+博弈)
    [JZOJ 5910] [NOIP2018模拟10.18] DuLiu 解题报告 (并查集+思维)
    [JZOJ 5852] [NOIP2018提高组模拟9.6] 相交 解题报告 (倍增+LCA)
    [JZOJ 5437] [NOIP2017提高A组集训10.31] Sequence 解题报告 (KMP)
    [JZOJ 5875] [NOIP2018提高组模拟9.20] 听我说,海蜗牛 解题报告(BFS+二分)
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681572.html
Copyright © 2011-2022 走看看