zoukankan      html  css  js  c++  java
  • UVA 11354 Bond

    Bond

    Time Limit: 8000ms
    Memory Limit: 131072KB
    This problem will be judged on UVA. Original ID: 11354
    64-bit integer IO format: %lld      Java class name: Main
     

    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 ≤ 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

    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

    Output for Sample Input

    20

    20

    100

    解题:LCA ,对最小生成树进行LCA查询

     1 #include <bits/stdc++.h>
     2 #define pii pair<int,int>
     3 using namespace std;
     4 const int maxn = 50010;
     5 int n,m;
     6 vector< pii >g[maxn];
     7 struct arc {
     8     int u,v,w;
     9     bool operator<(const arc &t)const {
    10         return w < t.w;
    11     }
    12 } e[200000];
    13 int uf[maxn],d[maxn],fa[maxn][21],maxcost[maxn][21];
    14 int Find(int x) {
    15     if(x != uf[x]) uf[x] = Find(uf[x]);
    16     return uf[x];
    17 }
    18 void dfs(int u,int f) {
    19     for(int i = g[u].size()-1; i >= 0; --i) {
    20         if(g[u][i].first == f) continue;
    21         d[g[u][i].first] = d[u] + 1;
    22         maxcost[g[u][i].first][0] = g[u][i].second;
    23         fa[g[u][i].first][0] = u;
    24         dfs(g[u][i].first,u);
    25     }
    26 }
    27 void init() {
    28     for(int j = 1; j <= 20; ++j)
    29         for(int i = 1; i <= n; ++i) {
    30             fa[i][j] = fa[fa[i][j-1]][j-1];
    31             maxcost[i][j] = max(maxcost[i][j-1],maxcost[fa[i][j-1]][j-1]);
    32         }
    33 }
    34 int LCA(int s,int t) {
    35     if(d[s] < d[t]) swap(s,t);
    36     int log,ret = INT_MIN;
    37     for(log = 1; (1<<log) <= d[s]; ++log);
    38     for(int i = log-1; i >= 0; --i)
    39         if(d[s] - (1<<i) >= d[t]) {
    40             ret = max(ret,maxcost[s][i]);
    41             s = fa[s][i];
    42         }
    43     if(s == t) return ret;
    44     for(int i = log-1; i >= 0; --i) {
    45         if(fa[s][i] != -1 && fa[s][i] != fa[t][i]) {
    46             ret = max(ret,maxcost[s][i]);
    47             ret = max(ret,maxcost[t][i]);
    48             s = fa[s][i];
    49             t = fa[t][i];
    50         }
    51     }
    52     ret = max(ret,maxcost[s][0]);
    53     ret = max(ret,maxcost[t][0]);
    54     return ret;
    55 }
    56 int main() {
    57     int u,v;
    58     bool flag = false;
    59     while(~scanf("%d%d",&n,&m)) {
    60         if(flag) puts("");
    61         flag = true;
    62         for(int i = 0; i < m; ++i)
    63             scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
    64         sort(e,e+m);
    65         for(int i = 0; i < maxn; ++i) {
    66             g[i].clear();
    67             uf[i] = i;
    68         }
    69         for(int i = 0; i < m; ++i) {
    70             int u = Find(e[i].u);
    71             int v = Find(e[i].v);
    72             if(u == v) continue;
    73             uf[u] = v;
    74             g[u].push_back(pii(v,e[i].w));
    75             g[v].push_back(pii(u,e[i].w));
    76         }
    77         memset(maxcost,0,sizeof maxcost);
    78         memset(d,0,sizeof d);
    79         memset(fa,-1,sizeof fa);
    80         fa[1][0] = -1;
    81         dfs(1,-1);
    82         init();
    83         scanf("%d",&m);
    84         for(int i = 0; i < m; ++i) {
    85             scanf("%d%d",&u,&v);
    86             printf("%d
    ",LCA(u,v));
    87         }
    88     }
    89     return 0;
    90 }
    View Code
  • 相关阅读:
    SourceTree 启动不需要账号
    获取httpservletrequest所有参数的键值对--key-value,用getParameterMap
    ajax 参数出现在Request payload导致request.getParameter(name)是获取不到值
    导出
    替换jar包里面的文件。利用jar ucf
    mysql CPU使用率过高的一次处理经历
    实用小知识
    spring-boot 静态资源自定义配置
    java多线程调用静态方法安全吗?
    spring 注解@Bean
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4699320.html
Copyright © 2011-2022 走看看