zoukankan      html  css  js  c++  java
  • poj 1986

    Distance Queries
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 8638   Accepted: 3032
    Case Time Limit: 1000MS

    Description

    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
    

    Hint

    Farms 2 and 6 are 20+3+13=36 apart. 

    Source

     
    lca
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <stack>
     6 #include <vector>
     7 
     8 using namespace std;
     9 
    10 const int MAX_N = 40005;
    11 int N,M;
    12 int first[MAX_N],Next[2 * MAX_N],v[2 * MAX_N];
    13 int id[MAX_N],vs[2 * MAX_N];
    14 int dep[MAX_N * 2],d[MAX_N * 2][30],qid[MAX_N * 2][30];
    15 int Dis[MAX_N],w[MAX_N * 2];
    16 int n;
    17 
    18 void RMQ() {
    19         for(int i = 1; i <= n; ++i) {
    20                 d[i][0] = dep[i];
    21                 qid[i][0] = i;
    22         }
    23 
    24         for(int j = 1; (1 << j) <= n; ++j) {
    25                 for(int i = 1; i + (1 << j) - 1 <= n; ++i) {
    26                         if(d[i][j - 1] > d[i + (1 << (j - 1))][j - 1]) {
    27                                 d[i][j] = d[i + (1 << (j - 1))][j - 1];
    28                                 qid[i][j] = qid[i + (1 << (j - 1))][j - 1];
    29                         } else {
    30                                 d[i][j] = d[i][j - 1];
    31                                 qid[i][j] = qid[i][j - 1];
    32                         }
    33                 }
    34         }
    35 
    36 }
    37 
    38 void add_edge(int id,int u) {
    39         int e = first[u];
    40         Next[id] = e;
    41         first[u] = id;
    42 }
    43 
    44 int  query(int L,int R) {
    45         int k = 0;
    46         while((1 << (k + 1)) < (R - L + 1)) ++k;
    47         return d[L][k] < d[R - (1 << k) + 1][k] ?
    48                qid[L][k] : qid[R - (1 << k) + 1][k];
    49 }
    50 
    51 void dfs(int u,int fa,int d,int dis,int &k) {
    52         id[u] = k;
    53         vs[k] = u;
    54         dep[k++] = d;
    55         Dis[u] = dis;
    56         for(int e = first[u]; e != -1; e = Next[e]) {
    57                 if(v[e] != fa) {
    58                         dfs(v[e],u,d + 1,dis + w[e],k);
    59                         vs[k] = u;
    60                         dep[k++] = d;
    61                 }
    62         }
    63 }
    64 
    65 int main()
    66 {
    67    // freopen("sw.in","r",stdin);
    68     scanf("%d%d",&N,&M);
    69     n = 2 * N - 1;
    70 
    71     for(int i = 1; i <= N; ++i) first[i] = -1;
    72     for(int i = 1; i <= 2 * M; i += 2) {
    73             int u;
    74             char ch;
    75             scanf("%d%d%d %c",&u,&v[i],&w[i],&ch);
    76             //printf("%d %d %d
    ",u,v[i],w[i]);
    77             w[i + 1] = w[i];
    78             v[i + 1] = u;
    79             add_edge(i,u);
    80             add_edge(i + 1,v[i]);
    81     }
    82 
    83     int k = 1;
    84     dfs(1,-1,0,0,k);
    85     RMQ();
    86 
    87     int Q;
    88     scanf("%d",&Q);
    89     for(int i = 1; i <= Q; ++i) {
    90             int a,b;
    91             scanf("%d%d",&a,&b);
    92             int p = vs[ query(min(id[a],id[b]),max(id[a],id[b])) ];
    93             printf("%d
    ",Dis[a] + Dis[b] - 2 * Dis[p]);
    94     }
    95 
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    Treap仿set 模板
    线段树(区间更改,区间查最值)模板
    UVA 12345 Dynamic len(带修莫队)
    服务器配置环境以及部署项目流程
    使用SSH的scp命令行传输文件到远程服务器
    服务器部署javaweb项目
    在ubuntu服务器上安装mysql并配置外网访问
    在ubuntu服务器上配置tomcat
    在ubuntu服务器上配置jdk
    linux命令--解压与压缩
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3712091.html
Copyright © 2011-2022 走看看