题意:给你一些路的首尾坐标,求从A路是否能到达B路
思路:先判断线段是否相交建图,然后求多源多点最短路即可,
AC代码:
#include <iostream> #include<string.h> #include<algorithm> #include<cstdio> #define CLR(arr,val) memset(arr,val,sizeof(arr)) using namespace std; #define N 105 #define inf 1<<30 #define Max(x, y)(x > y ? x : y) #define Min(x, y)(x < y ? x : y) typedef struct { double x1; double y1; double x2; double y2; }Node; typedef struct { double x; double y; }Point; Node node[N]; int map[N][N]; double Multi(Point p1, Point p2, Point p3)//叉乘判断点与线的位置关系 { return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y); } bool Isintersect(Point a1, Point a2, Point b1, Point b2)//判断两条线段是否相交(含顶点) { if (Min(a1.x, a2.x) <= Max(b1.x, b2.x) && Min(a1.y, a2.y) <= Max(b1.y, b2.y) && Min(b1.x, b2.x) <= Max(a1.x, a2.x) && Min(b1.y, b2.y) <= Max(a1.y, a2.y) && Multi(a1, a2, b1) * Multi(a1, a2, b2) <= 0 && Multi(b1, b2, a1) * Multi(b1, b2, a2) <= 0 ) return true;//说明两线段之间相交 return false; } void in(int &a) { char ch; while((ch=getchar())<'0'||ch>'9'); for( a=0;ch>='0'&&ch<='9';ch=getchar()) a=a*10+ch-'0'; } bool ok(int a,int b) { Point a1,b1,a2,b2; a1.x=node[a].x1,a1.y=node[a].y1; b1.x=node[a].x2,b1.y=node[a].y2; a2.x=node[b].x1,a2.y=node[b].y1; b2.x=node[b].x2,b2.y=node[b].y2; if(Isintersect(a1,b1,a2,b2)) return true; else return false; } int main() { int n,m; while(~scanf("%d",&n)) { for(int i=1;i<=n;++i) scanf("%lf%lf%lf%lf",&node[i].x1,&node[i].y1,&node[i].x2,&node[i].y2); for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) { if(ok(i,j)) map[i][j]=map[j][i]=1; else map[i][j]=map[j][i]=0; } in(m); for(int k=1;k<=n;++k) for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) if(map[k][j]&&map[j][i]) map[k][i]=map[i][k]=1; for(int i=0;i!=m;++i) { int a,b; in(a),in(b); if(map[a][b]) puts("YES"); else puts("NO"); } } return 0; }