I am mentally retarded, read and write the wrong tune for a long time not found =
Although is a shortest circuit but has the network flow general Shen Jiantu AH.
First found in the corner is the best turn, so the first discretization, and then the rectangle of the four vertices to its nearest to the next left and right point of the edge, run SPFA can.
is difficult to write Ah, but also to judge no solution: St in the rectangle; dis[t]=inf
#include <iostream>#include <cstdio>#include <queue>#include <cstring>#include <algorithm>#include <map>#include <cstdlib>using namespaceStdConst intn=2005;Const Long Longinf=1e15;intn,m,sx,sy,tx,ty,a[n],b[n],c[n],d[n],hax,hay,x[n],y[n],mk[n][n],tot,s,t,fx[n],fy[n],h[24005],cnt;Long Longdis[24005];BOOLv[n][n],fl[n],vis[24005];map<int,int>mx,my;structedg{intNe,to;Long LongVA;} e[200005];structqwe{intX,y,id; Qwe (intx=0,inty=0,intId=0) {x=x,y=y,id=id; }}p[24005];BOOLCMPX (ConstQwe &a,ConstQwe &b) {returna.x<b.x| | (A.X==B.X&&A.Y<B.Y);}BOOLCmpy (ConstQwe &a,ConstQwe &b) {returna.y<b.y| | (a.y==b.y&&a.x<b.x);}intRead () {intR=0, f=1;CharP=getchar (); while(p>' 9 '|| p<' 0 ') {if(p=='-') f=-1; P=getchar (); } while(p>=' 0 '&&p<=' 9 ') {r=r*Ten+p-48; P=getchar (); }returnR*f;}inline Long LongJL (Qwe a,qwe b) {returnABS (fx[a.x]-fx[b.x]) +abs (Fy[a.y]-fy[b.y]);}voidZouintXintYintDxintDY) {x+=dx,y+=dy; while(!mk[x][y]) {if(x<1|| x>hax| | y<1|| Y>hay)return; X+=dx,y+=dy; } v[x][y]=1;}inline voidAddintUintVLong LongW) {cnt++; E[cnt].ne=h[u]; E[cnt].to=v; E[cnt].va=w; h[u]=cnt;}inline voidInsintUintVLong LongW) {//cerr<<u<< "" <<v<< "" <<w<<endl;Add (U,V,W); Add (v,u,w);}intMain () {//int t=read (); intT scanf"%d", &t); while(t--) {mx.clear (), my.clear (); memset (H,0,sizeof(h)); memset (V,0,sizeof(v)); Memset (MK,0,sizeof(MK)); Memset (fl,0,sizeof(fl)); memset (Vis,0,sizeof(VIS)); Cnt=0; tot=0, hax=0, hay=0;//Sx=read (), Sy=read (), Tx=read (), Ty=read (), N=read ();scanf"%d%d%d%d%d", &sx,&sy,&tx,&ty,&n); for(intI=1; i<=n;i++) {//A[i]=read (), B[i]=read (), C[i]=read (), D[i]=read ();scanf"%d%d%d%d", &a[i],&b[i],&c[i],&d[i]);if(A[i]>c[i]) swap (a[i],c[i]);if(B[i]>d[i]) swap (b[i],d[i]); X[i]=a[i],x[i+n]=c[i],y[i]=b[i],y[i+n]=d[i]; } x[2*n+1]=sx,x[2*n+2]=tx,y[2*n+1]=sy,y[2*n+2]=ty; Sort (x+1X+1+2*n+2); Sort (y+1Y+1+2*n+2); for(intI=1; i<=2*n+2; i++)if(i==1|| X[i]!=x[i-1]) Mx[x[i]]=++hax,fx[hax]=x[i]; for(intI=1; i<=2*n+2; i++)if(i==1|| Y[i]!=y[i-1]) My[y[i]]=++hay,fy[hay]=y[i];//cerr<<y[i]<< "" <<my[y[i]]<<endl;Sx=mx[sx],sy=my[sy],tx=mx[tx],ty=my[ty]; mk[sx][sy]=mk[tx][ty]=-1; for(intI=1; i<=n;i++) for(intj=1; j<=n;j++)if(I!=j&&a[i]>=a[j]&&b[i]>=b[j]&&c[i]<=c[j]&&d[i]<=d[j]) {fl[i]=1; Break; } for(intI=1; i<=n;i++)if(!fl[i]) {A[i]=mx[a[i]],b[i]=my[b[i]],c[i]=mx[c[i]],d[i]=my[d[i]];//cerr<<a[i]<< "<<b[i]<<" "<<c[i]<<" "<<d[i]<<endl; for(intj=a[i];j<=c[i];j++) mk[j][b[i]]=1, mk[j][d[i]]=2; for(intj=b[i];j<=d[i];j++) mk[a[i]][j]=1, mk[c[i]][j]=2; mk[a[i]][b[i]]=mk[a[i]][d[i]]=mk[c[i]][b[i]]=mk[c[i]][d[i]]=-1; } for(intI=1; i<=n;++i)if((Sx>a[i]&&sy>b[i]&&sx<c[i]&&sy<d[i]) | | (Tx>a[i]&&ty>b[i]&&tx<c[i]&&ty<d[i])) {Puts ("No Path");return 0; } for(intI=1; i<=n;i++)if(!fl[i]) {Zou (A[i],b[i],-1,0), Zou (A[i],b[i],0,-1); Zou (A[i],d[i],-1,0), Zou (A[i],d[i],0,1); Zou (C[i],b[i],1,0), Zou (C[i],b[i],0,-1); Zou (C[i],d[i],1,0), Zou (C[i],d[i],0,1); v[a[i]][b[i]]=v[a[i]][d[i]]=v[c[i]][b[i]]=v[c[i]][d[i]]=1; } v[sx][sy]=v[tx][ty]=1; Zou (Sx,sy,-1,0), Zou (Sx,sy,1,0), Zou (Sx,sy,0,-1), Zou (Sx,sy,0,1); Zou (Tx,ty,-1,0), Zou (Tx,ty,1,0), Zou (Tx,ty,0,-1), Zou (Tx,ty,0,1); for(intI=1; i<=hax;i++) for(intj=1; j<=hay;j++)if(V[i][j]) {tot++; P[tot]=qwe (I,j,tot);if(I==sx&&j==sy) S=tot;if(I==tx&&j==ty) T=tot; }//cerr<<s<< "" <<t<<endl;Sort (p+1P+1+TOT,CMPX); for(intI=1, j;i<=tot;i=j) for(j=i+1; j<=tot&&p[j].x==p[i].x;j++)if(mk[p[j].x][p[j].y]==-1|| Mk[p[j-1].x][p[j-1].y]==-1|| Mk[p[j-1].x][p[j-1].Y]>=MK[P[J].X][P[J].Y]) ins (p[j-1].ID,P[J].ID,JL (p[j-1],P[J])); Sort (p+1P+1+tot,cmpy); for(intI=1, j;i<=tot;i=j) for(j=i+1; j<=tot&&p[j].y==p[i].y;j++)if(mk[p[j].x][p[j].y]==-1|| Mk[p[j-1].x][p[j-1].y]==-1|| Mk[p[j-1].x][p[j-1].Y]>=MK[P[J].X][P[J].Y]) ins (p[j-1].ID,P[J].ID,JL (p[j-1],P[J])); queue<int>q; for(intI=1; i<=tot;i++) Dis[i]=inf; dis[s]=0; vis[s]=1; Q.push (s); while(!q.empty ()) {intU=q.front (); Q.pop (); vis[u]=0; for(intI=h[u];i;i=e[i].ne)if(Dis[e[i].to]>dis[u]+e[i].va) {Dis[e[i].to]=dis[u]+e[i].va;if(!vis[e[i].to]) {vis[e[i].to]=1; Q.push (e[i].to); } } }if(Dis[t]==inf) puts ("No Path");Elseprintf"%lld\n", Dis[t]); }return 0;}
Bzoj 2304: [Apio2011] Seek the way "SPFA"