zoukankan      html  css  js  c++  java
  • POJ 1984 Navigation Nightmare

    Navigation Nightmare

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other)
    Total Submission(s) :    Accepted Submission(s) : 
    Problem Description
    Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 1000) connect the farms. A map of these farms might look something like the illustration below in which farms are labeled F1..F7 for clarity and lengths between connected farms are shown as (n): 
    F1 --- (13) ---- F6 --- (9) ----- F3
    |
    |
    (3)
    |
    | (7)
    F4 --- (20) -------- F2 |
    |
    |
    (2) F5

    F7 
    Being an ASCII diagram, it is not precisely to scale, of course. 

    Each farm can connect directly to at most four other farms via roads that lead exactly north, south, east, and/or west. Moreover, farms are only located at the endpoints of roads, and some farm can be found at every endpoint of every road. No two roads cross, and precisely one path 
    (sequence of roads) links every pair of farms. 

    FJ lost his paper copy of the farm map and he wants to reconstruct it from backup information on his computer. This data contains lines like the following, one for every road: 

    There is a road of length 10 running north from Farm #23 to Farm #17 
    There is a road of length 7 running east from Farm #1 to Farm #17 
    ... 

    As FJ is retrieving this data, he is occasionally interrupted by questions such as the following that he receives from his navigationally-challenged neighbor, farmer Bob: 

    What is the Manhattan distance between farms #1 and #23? 

    FJ answers Bob, when he can (sometimes he doesn't yet have enough data yet). In the example above, the answer would be 17, since Bob wants to know the "Manhattan" distance between the pair of farms. 
    The Manhattan distance between two points (x1,y1) and (x2,y2) is just |x1-x2| + |y1-y2| (which is the distance a taxicab in a large city must travel over city streets in a perfect grid to connect two x,y points). 

    When Bob asks about a particular pair of farms, FJ might not yet have enough information to deduce the distance between them; in this case, FJ apologizes profusely and replies with "-1". 
     

    Input
    * Line 1: Two space-separated integers: N and M

    * Lines 2..M+1: Each line contains four space-separated entities, F1,
    F2, L, and D that describe a road. F1 and F2 are numbers of
    two farms connected by a road, L is its length, and D is a
    character that is either 'N', 'E', 'S', or 'W' giving the
    direction of the road from F1 to F2.

    * Line M+2: A single integer, K (1 <= K <= 10,000), the number of FB's
    queries

    * Lines M+3..M+K+2: Each line corresponds to a query from Farmer Bob
    and contains three space-separated integers: F1, F2, and I. F1
    and F2 are numbers of the two farms in the query and I is the
    index (1 <= I <= M) in the data after which Bob asks the
    query. Data index 1 is on line 2 of the input data, and so on.
     

    Output
    * Lines 1..K: One integer per line, the response to each of Bob's
    queries. Each line should contain either a distance
    measurement or -1, if it is impossible to determine 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
    1 4 3
    2 6 6
     

    Sample Output
    13
    -1
    10
     

    Source
    PKU
     
    X,Y记录相对根节点的坐标,用并查集判断
     
     1 #include <cstdio>
     2 #include <math.h>
     3 
     4 #define MAXN 40040
     5 
     6 int n,m;
     7 int fa[MAXN];
     8 int X[MAXN];
     9 int Y[MAXN];
    10 
    11 int a[MAXN];
    12 int b[MAXN];
    13 int l[MAXN];
    14 char c[MAXN][5];
    15 
    16 void init()
    17 {
    18     for(int i=0;i<=n+10;i++)
    19     {
    20         X[i]=Y[i]=0;
    21         fa[i]=i;
    22     }
    23 }
    24 
    25 int Find(int i)//两个功能,1:判断是否在一个并查集里2:求坐标
    26 {
    27     if(i!=fa[i])
    28     {
    29         int t=fa[i];
    30         fa[i]=Find(t);
    31         X[i]+=X[t];
    32         Y[i]+=Y[t];
    33     }
    34     return fa[i];
    35 }
    36 
    37 void Union(int x,int y,int dx,int dy)
    38 {
    39     int rx=Find(x);
    40     int ry=Find(y);
    41     if(rx!=ry)
    42     {
    43         fa[ry]=rx;//把Y挂到X上。
    44         X[ry]=X[x]+dx-X[y];//以y点为中心更新结点Y的相对位置,X[ry]+X[y]=X[rx]+dx+X[x],在rx为根节点的情况下,X[rx]是为0的。
    45         Y[ry]=Y[x]+dy-Y[y];
    46     }
    47 }
    48 
    49 
    50 int main()
    51 {
    52     scanf("%d%d",&n,&m);
    53     init();
    54     for(int i=0;i<m;i++)
    55     {
    56        scanf("%d%d%d%s",&a[i],&b[i],&l[i],c[i]);
    57     }
    58     int k;
    59     scanf("%d",&k);
    60     int f1,f2,times;
    61     int cur=0;
    62     int i=0;
    63     while(k--)
    64     {
    65         scanf("%d%d%d",&f1,&f2,&times);
    66         for(i=cur;i<times;i++)
    67         {
    68             int dx=0,dy=0;
    69             switch(c[i][0])
    70             {
    71                 case 'N': dy+=l[i]; break;
    72                 case 'E': dx+=l[i]; break;
    73                 case 'S': dy-=l[i]; break;
    74                 case 'W': dx-=l[i]; break;
    75             }
    76             Union(a[i],b[i],dx,dy);
    77         }
    78         cur=i;
    79         if(Find(f1)!=Find(f2))  printf("-1\n");//这时候才把坐标算出来
    80         else
    81         {
    82             int ddx=fabs(X[f1]-X[f2]);
    83             int ddy=fabs(Y[f1]-Y[f2]);
    84             printf("%d\n",ddx+ddy);
    85         }
    86     }
    87     return 0;
    88 }
    89  
  • 相关阅读:
    站点目录中的文件夹被删除后,应用程序池被重启
    silverlight中UserControl的属性在xaml文件中敲不出来的问题
    提取自Discuz NT 的验证码生成
    Asp.net首页生成静态页的一个比较好的方法
    asp.net 字符串格式化
    阻止用户关闭网页,提示保存的解决方案IE/FF/OP通用(未经测试)
    .NET程序如何防止被注入(整站)
    好久没有进步了
    C#数组排序
    我的静态页面
  • 原文地址:https://www.cnblogs.com/CKboss/p/3066323.html
Copyright © 2011-2022 走看看