zoukankan      html  css  js  c++  java
  • BNUOJ 2105 Distance Queries

    Distance Queries

    Time Limit: 2000ms
    Memory Limit: 30000KB
    This problem will be judged on PKU. Original ID: 1986
    64-bit integer IO format: %lld      Java class name: Main
     
     
    Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle.  He therefore wants to find a path of a more reasonable length.  The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries".  Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms).  Please answer FJ's distance queries as quickly as possible!
     

    Input

    * Lines 1..1+M: Same format as "Navigation Nightmare"

    * Line 2+M: A single integer, K.  1 <= K <= 10,000

    * Lines 3+M..2+M+K: Each line corresponds to a distance query and  contains the indices of two farms.
     

    Output

    * Lines 1..K: For each distance query, output on a single line an integer giving  the appropriate distance.
     

    Sample Input

    7 6
    1 6 13 E
    6 3 9 E
    3 5 7 S
    4 1 3 N
    2 4 20 W
    4 7 2 S
    3
    1 6
    1 4
    2 6
    

    Sample Output

    13
    3
    36
    

    Source

     
    解题:LCA求树上任意两点间的距离。任意两点间只有一条路啊!不然那还是树么?^_^。。。。
     
    LCA(a,b) = c所以d(a,b) = d(a,root)+d(b,root)-2*d(c,root);人字形?嘻嘻!傻逼。。。。。当时我居然不能明白这个
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 #define INF 0x3f3f3f
    11 using namespace std;
    12 const int maxn = 100010;
    13 struct arc{
    14     int to,w;
    15 };
    16 struct query{
    17     int to,id;
    18 };
    19 int n,m,k,ans[maxn],d[maxn],uf[maxn];
    20 bool vis[maxn];
    21 vector<arc>g[maxn];
    22 vector<query>q[maxn];
    23 int Find(int x){
    24     if(x != uf[x])
    25         uf[x] = Find(uf[x]);
    26     return uf[x];
    27 }
    28 void tarjan(int u,int ds){
    29     vis[u] = true;
    30     d[u] = ds;
    31     uf[u] = u;
    32     int i;
    33     for(i = 0; i < g[u].size(); i++){
    34         if(!vis[g[u][i].to]) {tarjan(g[u][i].to,ds+g[u][i].w);uf[g[u][i].to] = u;}
    35     }
    36     for(i = 0; i < q[u].size(); i++)
    37     if(vis[q[u][i].to]){
    38         ans[q[u][i].id] = d[u]+d[q[u][i].to]-2*d[Find(q[u][i].to)];
    39     }
    40 }
    41 int main(){
    42     int i,j,u,v,w;
    43     char ch;
    44     while(~scanf("%d %d",&n,&m)){
    45         for(i = 0; i <= n; i++){
    46             g[i].clear();
    47             q[i].clear();
    48             d[i] = 0;
    49             vis[i] = false;
    50         }
    51         for(i = 0; i < m; i++){
    52             scanf("%d %d %d %c",&u,&v,&w,&ch);
    53             g[u].push_back((arc){v,w});
    54             g[v].push_back((arc){u,w});
    55         }
    56         scanf("%d",&k);
    57         for(i = 0; i < k; i++){
    58             scanf("%d %d",&u,&v);
    59             q[u].push_back((query){v,i});
    60             q[v].push_back((query){u,i});
    61         }
    62         tarjan(1,0);
    63         for(i = 0; i < k; i++)
    64             printf("%d
    ",ans[i]);
    65     }
    66     return 0;
    67 }
    View Code
     
     
  • 相关阅读:
    EasyARM-iMX283A的Linux 开发环境构建
    linux指令tar笔记
    使用cuteFTP与虚拟机交互文件---安装ftp服务
    SecureCRT显示乱码的解决办法
    【转】简明 Vim 练级攻略
    图像识别___YUV学习手记
    一个简易的软件定时器
    OV7670配置和调试小结
    linux驱动开发( 五) 字符设备驱动框架的填充file_operations结构体中的操作函数(read write llseek unlocked_ioctl)
    hash-1.hash表和hash算法
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3897409.html
Copyright © 2011-2022 走看看