Description
give you the distance and direction of each point and the neighboring point, and find the Manhattan distance between two points. \ (n \leqslant 4\times 10^4\).
Sol
Weighted and check set.
Merging like a vector can be done, looking for \ (f[x]\) to record the current parent node, and then update his new parent node.
Code
/************************************************************** problem:3362 User:beiyu language:c++ Result : Accepted time:80 ms memory:3712 kb****************************************************************/#include <cs tdio> #include <algorithm> #include <iostream>using namespace std; typedef pair< INT,INT > PR; #define ABS (x) ((x) <0?-(x): (x)) #define MPR Make_pair#define Debug (a) cout<< #a << "=" <<a<< "" Const int N = 40005; int N,m,k;int F[n],ans[n];p r g[n];struct q{int a,b,c,d;} Q[n],qq[n]; inline int in (int x=0,char Ch=getchar ()) {while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar (); return x;} inline Char Read (char Ch=getchar ()) {while (ch> ' Z ' | | ch< ' A ') Ch=getchar (); return ch;} PR operator + (const PR &A,CONST PR &b) {return MPR (A.first+b.first,a.second+b.second);} PR operator + = (pr &a,const PR &b) {return a=a+b }PR operator-(const PR &A,CONST PR &b) {return MPR (A.first-b.first,a.second-b.second);} PR Operator-= (pr &a,const PR &b) {return a=a-b;} int cmp1 (const q &x,const q &y) {return x.c<y.c;} int CMP2 (const q &x,const q &y) {return x.d<y.d;} int find (int x) {if (f[x] = = x) return x; int T=f[x];f[x]=find (f[x]); G[x]+=g[t];return f[x];} void work (int u,int V,int W,char d) {int r1=find (u), R2=find (v);//debug (R1), debug (R2) <<endl;//cout<<u< ;< "" <<v<< "<<w<<" "<<d<<endl;//cout<<" Qwq "<<endl; if (R1!=R2) switch (d) {case ' N ': F[r1]=r2,g[r1]=g[v]+mpr (0,w)-g[u];break; Case ' W ': F[r1]=r2,g[r1]=g[v]+mpr (-w,0)-g[u];break; Case ' S ': F[r1]=r2,g[r1]=g[v]+mpr (0,-w)-g[u];break; DEFAULT:F[R1]=R2,G[R1]=G[V]+MPR (w,0)-g[u];break; }//for (int i=1;i<=n;i++) cout<<find (i) << ":(" <<g[i].first<< "," <<g[i].second< < ")"; cout<≪endl;} int main () {//Freopen ("In.in", "R", stdin); N=in (), M=in (); for (int i=1;i<=n;i++) f[i]=i; for (int i=1;i<=m;i++) qq[i].a=in (), Qq[i].b=in (), Qq[i].c=in (), Qq[i].d=read (); K=in (); for (int i=1;i<=k;i++) q[i].a=in (), Q[i].b=in (), Q[i].c=in (), q[i].d=i; Sort (Q+1,Q+K+1,CMP1);//for (Int. i=1;i<=m;i++) work (QQ[I].A,QQ[I].B,QQ[I].C,QQ[I].D);//for (int i=1;i<=n;i++) Cout<<find (i) << "(" <<g[i].first<< "," <<g[i].second<< ") \ n"; for (int. i=1,j=1,u,v,r1,r2;i<=k;i++) {for (; j<=q[i].c && j<=n;++j) Work (qq[j].a,qq[j].b,qq[j].c,qq[j ].D); U=q[i].a,v=q[i].b,r1=find (U), R2=find (v); if (R1==R2) {//debug (U), Debug (v), Debug (G[u].first), Debug (G[v].first), Debug (G[u].second), Debug (G[v].second) <& Lt;endl; Ans[q[i].d]=abs (G[u].first-g[v].first) +abs (G[u].second-g[v].second); }else Ans[q[i].d]=-1; }//for (int i=1;i<=n;i++) cout<<find (i) << "(" <<G[I].FIRST<≪ "," <<g[i].second<< ") \ n"; for (int i=1;i<=k;i++) printf ("%d\n", Ans[i]); return 0;}
Bzoj 3362: [Usaco2004 feb]navigation Nightmare navigation nightmare