To find the distance between two points, given the position relationship between the points
This problem tyrants, but POJ also have a same problem, you can brush
Don't be subject to the problem, this problem does not force online, the query offline processing can be
And then there's the question of the right and the collection. Set weights to directional vectors, overload + and-, follow normal weights and check the set.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 40400
using namespace Std;
struct abcd{int x,y;
ABCD () {} abcd (int x,int Y): X (x), Y (y) {} ABCD operator + (const ABCD &y) const {return ABCD (x+y.x, Y+Y.Y);
ABCD operator-(const ABCD &y) const {return ABCD (x-y.x, Y-Y.Y);
}}f[m];
struct operation{int x,y;
ABCD temp;
}OPERATIONS[M];
struct query{int x,y,z,pos;
BOOL operator < (const query &y) Const {return Z < y.z;
}}queries[10100];
int n,m,q,fa[m],ans[10100]; int Distance (ABCD x) {return abs (x.x) +abs (x.y);} int find (int x) {if (!fa[x]| |
FA[X]==X) return fa[x]=x;
int y=fa[x];
Fa[x]=find (Fa[x]);
F[X]=F[Y]+F[X];
return fa[x];
int main () {int i,j,x,y,z;
Char p[10];
cin>>n>>m;
for (i=1;i<=m;i++) {scanf ("%d%d%d%s", &operations[i].x,&operations[i].y,&z,p);
Switch (p[0]) {case ' E ': OPERATIONS[I].TEMP=ABCD (z,0); Case ' W ': OperatIONS[I].TEMP=ABCD (-z,0);
Case ' N ': OPERATIONS[I].TEMP=ABCD (0,z);
Case ' S ': OPERATIONS[I].TEMP=ABCD (0,-z);
}} cin>>q;
for (i=1;i<=q;i++) scanf ("%d%d%d", &queries[i].x,&queries[i].y,&queries[i].z), queries[i].pos=i;
Sort (queries+1,queries+q+1);
for (i=1,j=1;i<=q;i++) {for (; j<=queries[i].z;j++) {int x=operations[j].x;
int y=operations[j].y;
int Fx=find (x), Fy=find (y);
FA[FY]=FX;
F[fy]=f[x]-f[y]+operations[j].temp;
int x=queries[i].x;
int y=queries[i].y;
if (find (x)!=find (y)) ans[queries[i].pos]=-1;
else Ans[queries[i].pos]=distance (F[x]-f[y]);
for (i=1;i<=q;i++) printf ("%d\n", Ans[i]);
}