bzoj_3362_[usaco2004 Feb]navigation Nightmare Navigation nightmare _ and check set

Source: Internet
Author: User

bzoj_3362_[usaco2004 Feb]navigation Nightmare Navigation nightmare _ and check set

Descriptionfarmer John had N (2≤n≤40000) a farm, a different vertical or water marking 1 to n,m (2≤m≤40000)The flat roads are connected to the farm and the roads are not more than 1000 in length. These farms are distributed just like the map below,the picture of the farm with F1. F7 says that each farm can connect up to 4 different farms in the East four directions. Moreover, the farm is only at the ends of the road. Roads do not intersect and there is only one path between each pair of farms. Neighbor Bob offered John to navigate, but John lost the map of the farm and had to be repaired from a computer backup. The information for each road is as follows:from Farm 23 to South by distance 10 to farmfrom farm 1 to east longitude distance 7 to farmwhen John re-obtained the data, he was sometimes interrupted by Bob's question: "What is the distance between Farm 1 and farm 23 in Manhattan?" The so-called "Manhattan Distance" between (Xi,yi) and (X2,y2) is Lxl-x21+lyl-y21. If there is enough information, John will answer the question (the answer in the previous example is 17), otherwise he will be sincerely sorry and answer -1.InputLine 1th: two separate integers n and M.2nd to m+1: Each line consists of 4 separate contents, F1,F2, III, D respectively, describing the number of two farms, the length of the road, F1 to F2 direction n,e,s,w. line m+2: An integer, K (1≤k≤10000), indicating the number of questions. Line m+3 to m+k+2: Each line represents a problem, consisting of 3 parts: Fi,f2,,. Among them, FI and F2 said two were asked about the farm. and/(1≤j≤m) represents the moment when the question is raised. J is 1 o'clock, indicating that information 1 but not know the information 2 o'clock. Outputline 1th to K: one integer per line, answering the question. Represents a Manhattan distance between two farms. The output -1 is unknown.Sample Input7 6
1 6 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 W
4 7 2 S
3
1 6 1
1 4 3
2 6 6Sample Output13
-1
Ten The inquiry will be sorted, each inserted. The horizontal distance and longitudinal distance between ancestors and ancestors are maintained by using and checking the set, which is updated when find. Code:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define N 40050int N,fa [N],xx[n],yy[n],m,ans[n];char opt[10];int Abs (int x) {return x>0?x:-x;}    struct E {int a,b,c,d;        Void Rd () {scanf ("%d%d%d%s", &a,&b,&c,opt);        if (opt[0]== ' N ') d=1;        if (opt[0]== ' S ') d=2;        if (opt[0]== ' W ') d=3;    if (opt[0]== ' E ') d=4;    }}e[n];struct Q {int t,x,y,id;    Void Rd () {scanf ("%d%d%d", &x,&y,&t);}    BOOL operator < (const Q &u) Const {return t<u.t;    }}q[n];int find (int x) {if (fa[x]==x) return x;    int tmp=fa[x];    Fa[x]=find (Fa[x]);    XX[X]+=XX[TMP];    YY[X]+=YY[TMP]; return fa[x];}    void Add (int x) {int a=e[x].a,b=e[x].b,c=e[x].c,d=e[x].d;    int Da=find (a), db=find (b);    fa[da]=db;    if (ta! = tb) F[ta] = TB, dx[ta] = dx[b[t]] + cx[t]-dx[a[t]], dy[ta] = dy[b[t]] [cy[t]-dy[a[t]];        if (d==1) {xx[da]=xx[b]-xx[a];    Yy[da]=yy[b]-yy[a]-c; }else if (d==2) {xx[da]=xx[b]-xx[a];    Yy[da]=yy[b]-yy[a]+c;        }else if (d==3) {xx[da]=xx[b]-xx[a]-c;    Yy[da]=yy[b]-yy[a];        }else {xx[da]=xx[b]-xx[a]+c;    Yy[da]=yy[b]-yy[a];    }}int query (int x,int y) {int dx=find (x), Dy=find (y);    if (Dx!=dy) return-1; Return Abs (Xx[x]-xx[y]) +abs (Yy[x]-yy[y]);}    int main () {scanf ("%d%*d", &n);    int i;    for (i=1;i<=n;i++) fa[i]=i;    for (i=1;i<n;i++) {e[i].rd ();    } scanf ("%d", &m);    for (i=1;i<=m;i++) {q[i].rd (); q[i].id=i;    } sort (q+1,q+m+1);    int now=1;        for (i=1;i<=m;i++) {while (now<=q[i].t) {add [now]; now++;    } ans[q[i].id]=query (Q[I].X,Q[I].Y);    } for (i=1;i<=m;i++) {printf ("%d\n", Ans[i]); }}

bzoj_3362_[usaco2004 Feb]navigation Nightmare Navigation nightmare _ and check set

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.