Navigation Nightmare POJ - 1984

Navigation Nightmare

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.

题目:回答你一连串牧场之间的距离,在一连串回答中,中间穿插一些问题,问能不能确定x,y之间的距离,
可以得话输出答案,不可以则输出'-1'。
思路:我们容易想到用坐标处理“曼哈顿距离”,带权并查集有类似向量的性质,刚好和坐标可以对应,
我们就可以用带权并查集来处理该问题,
每个点两个权值,分别表示x坐标和y坐标,初始化为(0,0),然后四个方向也可以用坐标表示,这样题目就可以解决了。
  1 #include 
  2 #include 
  3 #include 
  4 #include 
  5 #include 
  6 #include 
  7 
  8 using namespace std;
  9 
 10 #define ll long long
 11 #define pb push_back
 12 #define fi first
 13 #define se second
 14 
 15 const int N = 4e4 + 10;
 16 struct node
 17 {
 18     int rt, x, y;
 19 }fa[N];
 20 struct info
 21 {
 22     int x, y, d;
 23     char op;
 24 };
 25 struct que
 26 {
 27     int x, y, inx;
 28 };
 29 vector Info;
 30 vector Que;
 31 int n, m, q;
 32 
 33 int Find(int x){
 34     if(fa[x].rt == x) return x;
 35     else{
 36         int tmp = fa[x].rt;
 37         fa[x].rt = Find(tmp);
 38         fa[x].x += fa[tmp].x;
 39         fa[x].y += fa[tmp].y;
 40         return fa[x].rt;
 41     }
 42 }
 43 
 44 void Union(int x, int y, int dx, int dy){
 45     int fax = Find(x);
 46     int fay = Find(y);
 47 
 48     if(fax != fay){
 49         fa[fay].rt = fax;
 50         fa[fay].x = fa[x].x + dx - fa[y].x;
 51         fa[fay].y = fa[x].y + dy - fa[y].y;
 52     }
 53 }
 54 
 55 void solve()
 56 {   
 57     //while(~scanf("%d%d", &n, &m)){
 58         scanf("%d%d", &n, &m);
 59         for(int i = 0; i <= n; ++i){
 60             fa[i].rt = i;
 61             fa[i].x = fa[i].y = 0;
 62         }
 63         Info.clear();
 64         Que.clear();
 65 
 66         int u, v, d, inx;
 67         char op[5];
 68         for(int i = 1; i <= m; ++i){
 69             scanf("%d%d%d%s", &u, &v, &d, op);
 70             Info.pb({u, v, d, op[0]});
 71         }
 72         scanf("%d", &q);
 73         for(int i = 1; i <= q; ++i){
 74             scanf("%d%d%d", &u, &v, &inx);
 75             Que.pb({u, v, inx});
 76         }
 77         //sort(Que.begin(), Que.end());
 78 
 79         vector<int > ans;
 80         int j = 0;
 81         for(int i = 0; i < q; ++i){
 82   
 83             while(j < Que[i].inx){
 84                 int dx = 0;
 85                 int dy = 0;
 86                 if(Info[j].op == 'E') dx = Info[j].d;
 87                 else if(Info[j].op == 'W') dx = -Info[j].d;
 88                 else if(Info[j].op == 'N') dy = Info[j].d;
 89                 else if(Info[j].op == 'S') dy = -Info[j].d;
 90                 
 91                 Union(Info[j].x, Info[j].y, dx, dy);
 92                 j++;
 93             }
 94 
 95             int fax = Find(Que[i].x);
 96             int fay = Find(Que[i].y);
 97             if(fax != fay) ans.pb(-1);
 98             else{
 99                 int dx = fa[Que[i].x].x - fa[Que[i].y].x;
100                 int dy = fa[Que[i].x].y - fa[Que[i].y].y;
101                 ans.pb(abs(dx) + abs(dy));
102             }
103         }
104     
105 
106         //for(int o = 0; o < l; ++o) printf("ans = %d\n", ans[o]);
107         for(int o = 0; o < q; ++o) printf("%d\n", ans[o]);
108     //}
109 }
110 
111 int main()
112 {
113 
114     solve();
115 
116     return 0;
117 }

 

 

你可能感兴趣的:(Navigation Nightmare POJ - 1984)