引用自此,别手贱删了。
#include <bits/stdc++.h>
using namespace std;
#define maxn 2500005
#define endl '\n'
typedef long long ll;
namespace Basic
{
inline int min(const int &x,const int &y){return x<y?x:y;}
inline ll min(const ll &x,const ll &y){return x<y?x:y;}
inline int max(const int &x,const int &y){return x>y?x:y;}
inline ll max(const ll &x,const ll &y){return x>y?x:y;}
inline void swap(int &x,int &y){x^=y^=x^=y;}
inline void swap(ll &x,ll &y){x^=y^=x^=y;}
}
using namespace Basic;
// 图论部分
namespace Graph
{
// 链式前向星存图
int cnt=0,head[maxn];
ll dis[maxn];
#define llinf 0x7f7f7f7f7f7f7f7fll
#define inf 0x7f7f7f7f
bool vis[maxn];
struct Edge
{
int to,next;
ll w;
} edge[maxn];
// 添加一条单向边
inline void AddEdge(const int &u,const int &v,const ll &w)
{
edge[++cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt;
}
// Dijkstra 使用的结点
struct DijNode
{
ll dis;
int pos;
inline bool operator <(const DijNode &x) const
{
return x.dis<dis;
}
};
priority_queue <DijNode> q;
// 最短路,结果存在 dis[i] 中
inline void Dijkstra(const int &start)
{
dis[start]=0;
q.push((DijNode){0,start});
while(!q.empty())
{
DijNode t=q.top();
q.pop();
int x=t.pos,d=t.dis;
if(vis[x])
continue;
vis[x]=1;
for(int i=head[x];i;i=edge[i].next)
{
int y=edge[i].to;
if(dis[y]>dis[x]+edge[i].w)
{
dis[y]=dis[x]+edge[i].w;
if(!vis[y])
q.push((DijNode){dis[y],y});
}
}
}
}
}
namespace Problem
{
int T,n;
class Point
{
public:
int x,y;
Point()
{
x=y=0;
}
Point(const int &x_,const int &y_)
{
x=x_,y=y_;
}
inline void reset(const int &x_,const int &y_)
{
x=x_,y=y_;
}
inline friend istream& operator >>(istream& cin,Point &p)
{
return (cin >> p.x >> p.y);
}
inline friend ostream& operator <<(ostream& cout,const Point &p)
{
return (cout << "(" << p.x << "," << p.y << ")");
}
inline bool operator <(const Point &B) const
{
if(x != B.x)
return x<B.x;
else
return y<B.y;
}
};
inline bool operator ==(const Point &A,const Point &B)
{
return A.x==B.x && A.y==B.y;
}
// S,E 为起点、终点,A[i] 和 B[i] 为第 i 个长方形的端点
Point S,E,A[1005],B[1005];
// coord[id][i] 为第 id 个长方形第 i 条边上的点
/* 边的编号
A--0--C
| |
3 1
| |
D--2--B
*/
set <Point> coord[1005][4];
// 两点距离公式,本题无浮点数
inline int Dist(const Point &A,const Point &B)
{
// 避免精度损失,特判(?)
if(A.x==B.x)
return abs(A.y-B.y);
else if(A.y==B.y)
return abs(A.x-B.x);
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
// 离散化,id[A] 表示 (A.x, A.y) 在图中结点编号
int cnt=0;
map <Point,int> id;
// 返回 (A.x, A.y) 在图中结点编号,没有的建立新点
inline int PointID(const Point &A)
{
if(id[A]!=0)
return id[A];
++cnt;
return id[A]=cnt;
}
// 两点是否连接过
map <pair<Point,Point>,bool> connected;
// 在几何图形中连接两点,边权为其几何距离
inline void Connect(const Point &A,const Point &B)
{
if(A==B||connected[(pair<Point,Point>){A,B}]||connected[(pair<Point,Point>){B,A}])
return;
connected[(pair<Point,Point>){A,B}]=connected[(pair<Point,Point>){B,A}]=1;
//printf("Connected (%d,%d) and (%d,%d), distance=%d\n",A.x,A.y,B.x,B.y,Dist(A,B));
Graph::AddEdge(PointID(A),PointID(B),Dist(A,B));
Graph::AddEdge(PointID(B),PointID(A),Dist(A,B));
}
// 连接点 Pt 的边,时间复杂度 O(n)
inline void Make(const Point &Pt)
{
int mindx[3]={inf,inf,inf},mindy[3]={inf,inf,inf};
int a1=inf,a2=inf,b1=inf,b2=inf;
for(int i=1;i<=n;++i)
{
if(A[i].x <= Pt.x && Pt.x <= B[i].x)
{
if(Pt.y<B[i].y)
{
const Point P(Pt.x,B[i].y);
if(Dist(Pt,P)<mindx[1])
{
mindx[1]=Dist(Pt,P);
a1=i;
}
}
else if(Pt.y>A[i].y)
{
const Point P(Pt.x,A[i].y);
if(Dist(Pt,P)<mindx[2])
{
mindx[2]=Dist(Pt,P);
a2=i;
}
}
}
if(B[i].y <= Pt.y && Pt.y <= A[i].y)
{
if(Pt.x<A[i].x)
{
const Point P(A[i].x,Pt.y);
if(Dist(Pt,P)<mindy[1])
{
mindy[1]=Dist(Pt,P);
b1=i;
}
}
else if(Pt.x>B[i].x)
{
const Point P(B[i].x,Pt.y);
if(Dist(Pt,P)<mindy[2])
{
mindy[2]=Dist(Pt,P);
b2=i;
}
}
}
}
Point P(0,0);
if(a1!=inf)
{
P.reset(Pt.x,B[a1].y);
Connect(P,Pt);
coord[a1][2].insert(P);
}
if(a2!=inf)
{
P.reset(Pt.x,A[a2].y);
Connect(P,Pt);
coord[a2][0].insert(P);
}
if(b1!=inf)
{
P.reset(A[b1].x,Pt.y);
Connect(P,Pt);
coord[b1][3].insert(P);
}
if(b2!=inf)
{
P.reset(B[b2].x,Pt.y);
Connect(P,Pt);
coord[b2][1].insert(P);
}
}
// 初始化
inline void Init()
{
memset(Graph::dis,0x7f7f,sizeof(Graph::dis));
memset(Graph::vis,0,sizeof(Graph::vis));
Graph::cnt=cnt=0;
while(!Graph::q.empty()) Graph::q.pop();
memset(Graph::head,0,sizeof(Graph::head));
memset(Graph::edge,0,sizeof(Graph::edge));
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
for(int i=0;i<=n+1;++i)
for(int p=0;p<4;++p)
coord[i][p].clear();
id.clear();
connected.clear();
}
}
using namespace Problem;
int main()
{
ios::sync_with_stdio(0);
cin.tie(nullptr),cout.tie(nullptr);
cin >> T;
while(T--)
{
cin >> S >> E >> n;
Init();
if(n==0)
{
if(S.x==E.x||S.y==E.y)
cout << Dist(S,E) << endl;
else
cout << "No Path" << endl;
continue;
}
if(S.x==E.x && S.y==E.y)
{
cout << 0 << endl;
continue;
}
// 输入
for(int i=1;i<=n;++i)
{
cin >> A[i] >> B[i];
// 保证:A.x < B.x 且 A.y > B.y
if(A[i].x > B[i].x)
swap(A[i].x,B[i].x);
if(A[i].y < B[i].y)
swap(A[i].y,B[i].y);
const Point Ci(B[i].x,A[i].y),Di(A[i].x,B[i].y);
coord[i][0].insert(A[i]);
coord[i][1].insert(Ci);
coord[i][2].insert(B[i]);
coord[i][3].insert(Di);
}
// 长方形之间的连边
for(int i=1;i<=n;++i)
{
const Point Ci(B[i].x,A[i].y),Di(A[i].x,B[i].y);
const Point pts[4]={A[i],Ci,B[i],Di};
for(int p=0;p<4;++p)
Make(pts[p]);
}
Make(S);
Make(E);
// 长方形边上的连边,顺便判断 S,E 是否可以直接连接(direct)
bool direct=(S.x==E.x||S.y==E.y);
for(int i=1;i<=n;++i)
{
const Point Ci(B[i].x,A[i].y),Di(A[i].x,B[i].y);
const Point pts[4]={Ci,B[i],Di,A[i]};
for(int p=0;p<4;++p)
{
coord[i][p].insert(pts[p]);
auto last=coord[i][p].begin();
// 这里的 auto:set<Point>::iterator
for(auto it=coord[i][p].begin();it!=coord[i][p].end();++it)
{
if(it!=coord[i][p].begin())
Connect(*last,*it);
last=it;
}
}
if(S.x==E.x && min(S.y,E.y) <= B[i].y && A[i].y <= max(S.y,E.y) && A[i].x <= S.x && S.x <= B[i].x)
direct=0;
if(S.y==E.y && min(S.x,E.x) <= A[i].x && B[i].x <= max(S.x,E.x) && B[i].y <= S.y && S.y <= A[i].y)
direct=0;
}
if(direct)
Connect(S,E);
// 跑最短路 Dij O(n^2)
Graph::Dijkstra(PointID(S));
if(Graph::dis[PointID(E)]==llinf)
cout << "No Path" << endl;
else
cout << Graph::dis[PointID(E)] << endl;
}
return 0;
}