Ah ... A question of comparing water. Discretization first, and then run the shortest way directly.
Note that it can walk from the discretized edge. So to deal with the edge (you can see my code, the code is annotated with comments) ... Because of this WA.
#include <queue>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Constsize_t Max_n ( the); typedef pair<int,int>pil;Const intDx[] = {+1, -1, +0, +0};Const intDy[] = {+0, +0, +1, -1};structsquare{intSX, Sy, TX, Ty;};intN;square V[max_n];intSX, SY, TX, TY;intA, B;DoubleX[max_n *3], Y[max_n *3];BOOLMap[max_n *3][max_n *3];intLookupConst int&n,Const DoubleArray[],Const Double&Key) { intL1), R (n +1), Mid; while(L <r) {Mid= L + ((r-l) >>1); if(Array[mid] = =Key)returnmid; if(Array[mid] <Key) L= Mid +1; ElseR=mid; } return-1;}voidinit () {intC; scanf ("%d", &N); for(inti =1; I <= n;++i) {scanf ("%d%d%d", &v[i].sx, &v[i].sy, &b); V[i].tx= V[i].sx + C, v[i].ty = V[i].sy +C; X[i*3-2] = V[I].SX, X[i *3-1] = V[i].tx, X[i *3] = (V[I].SX + v[i].tx)/2.0; Y[i*3-2] = v[i].sy, Y[i *3-1] = V[i].ty, Y[i *3] = (v[i].sy + v[i].ty)/2.0; } scanf ("%d%d%d%d", &SX, &sy, &TX, &TY); X[n*3+1] = SX, X[n *3+2] = TX, X[n *3+3] = (SX + TX)/2.0; Y[n*3+1] = SY, Y[n *3+2] = TY, Y[n *3+3] = (SY + TY)/2.0;}voidMake_graph () {sort (X+1, X +1+ N *3+3); Sort (Y+1, Y +1+ N *3+3); A= (Unique (X +1, X +1+ N *3+3)-X)-1; B= (Unique (Y +1, Y +1I3+3)-Y)-1; for(inti =1; I <= n;++i) {v[i].sx= Lookup (A, X, V[I].SX *1.0), V[i].sy = Lookup (B, Y, V[i].sy *1.0); V[i].tx= Lookup (A, X, V[i].tx *1.0), V[i].ty = Lookup (B, Y, V[i].ty *1.0); } SX= Lookup (A, X, SX *1.0), Sy = lookup (B, Y, SY *1.0); TX= Lookup (A, X, TX *1.0), Ty = Lookup (B, Y, Ty *1.0); for(inti =1; I <= n;++i) { for(intj = V[i].sy;j <= v[i].ty;++j) Map[v[i].sx][j]= Map[v[i].tx][j] =true; for(intj = V[i].sx;j <= v[i].tx;++j) Map[j][v[i].sy]= Map[j][v[i].ty] =true; }}intDist[max_n *3][max_n *3];BOOLIn_q[max_n *3][max_n *3];voidSPFA () {++a, ++b;//Processing Edgememset (Dist,0X3F,sizeof(Dist)); Queue<pil>Q; Q.push (Make_pair (SX, SY)); Dist[sx][sy]=0, In_q[sx][sy] =true; intTopX, Topy, ToX, ToY; while(Q.size ()) {TopX= Q.front (). First, topy =Q.front (). Second; In_q[topx][topy]=false; Q.pop (); for(intK =0; K! =4;++k) {ToX= TopX + Dx[k], ToY = topy +Dy[k]; if(! (ToX >=1&& ToX <= A && ToY >=1&& ToY <=B))Continue; if(Dist[tox][toy] > Dist[topx][topy] +Map[tox][toy]) {Dist[tox][toy]= Dist[topx][topy] +Map[tox][toy]; if(!In_q[tox][toy]) {In_q[tox][toy]=true; Q.push (Make_pair (ToX, ToY)); } }}} printf ("%d", Dist[tx][ty]);}intMain () {init (); Make_graph (); SPFA (); return 0;}
Bzoj 1967
Bzoj 1967 [Ahoi2005]cross through the magnetic field