POJ 1984 Navigation Nightmare two-dimensional belt right and search set

Source: Internet
Author: User

Title Source: POJ 1984 Navigation Nightmare

Test instructions: give you a tree K-times ask for a Manhattan distance between 2 points and have to start with a K-bar

Idea: In accordance with the direction of the upper left corner of the root of the top left corner is the origin dx[i] for I point distance root x coordinate dy[] is the Y coordinate two can be obtained by path compression is only two-dimensional

#include <cstdio> #include <cstdlib> #include <cstring>using namespace std;const int maxn = 40010;int f[ MAXN], DX[MAXN], dy[maxn];struct node{int u, V, W;char s[3];} A[maxn];void init (int n) {for (int i = 1; I <= n; i++) F[i] = i;memset (dx, 0, sizeof (DX)), memset (dy, 0, sizeof (DY));} int find (int x) {if (x! = F[x]) {int rt = find (F[x]);d x[x] + = dx[f[x]];d y[x] + = dy[f[x]];f[x] = Rt;return RT;} return x;} int main () {int n, m;scanf ("%d%d", &n, &m), Init (n), for (int i = 1; I <= m; i++) {scanf ("%d%d%d%s", &a[i].u , &AMP;A[I].V, &AMP;A[I].W, A[I].S);} int q;scanf ("%d", &q); int i = 1;while (q--) {int S, E, w;scanf ("%d%d%d", &s, &e, &w); while (I <= M & & I <= w) {int u = a[i].u;int v = a[i].v;int x = Find (u), int y = find (v), if (x = = y) continue;if (a[i].s[0] = = ' E ') {f[y ] = X;dx[y] = Dx[u]-dx[v]+a[i].w;dy[y] = Dy[u]-dy[v];} else if (a[i].s[0] = = ' W ') {f[x] = y;dx[x] = dx[v]-dx[u]+a[i].w;dy[x] = Dy[v]-dy[u];} else if (a[i].s[0] = = ' N ') {f[x] = y;dy[x] = dy[V]-dy[u]+a[i].w;dx[x] = Dx[v]-dx[u];} Else{f[y] = X;dy[y] = Dy[u]-dy[v]+a[i].w;dx[y] = Dx[u]-dx[v];} i++;} int x = find (s), int y = Find (E), if (x = = y) {int d = ABS (Dx[s]-dx[e]) + ABS (Dy[s]-dy[e]);p rintf ("%d\n", d);} Else{puts ("1");}} return 0;}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.