判断两点间连通性,建图跑floyed
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=105; 4 struct node 5 { 6 double z[5],x; 7 }q[N];double e[N][N]; 8 double dis(double x1,double y1,double x2,double y2) 9 { 10 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 11 } 12 bool come(int i,int j,int a,int b) 13 { 14 if(j-i<2)return 0; 15 double x1=q[i].x,x2=q[j].x,y1=q[i].z[a],y2=q[j].z[b];if(x1==x2)return 1; 16 double k=(y1-y2)/(x1-x2),bb=y1-x1*k; 17 for(i=i+1;i<j;++i) 18 { 19 double y=q[i].x*k+bb; 20 if(y<q[i].z[1]||(y>q[i].z[2]&&y<q[i].z[3])||y>q[i].z[4])return 1; 21 } 22 return 0; 23 } 24 void add(int i,int j,int a,int b) 25 { 26 if(come(i,j,a,b))return; 27 e[i*4+a][j*4+b]=e[j*4+b][i*4+a]=dis(q[i].x,q[i].z[a],q[j].x,q[j].z[b]); 28 } 29 int main() 30 { 31 int n; 32 scanf("%d",&n); 33 for(int i=1;i<=n;++i) 34 { 35 scanf("%lf",&q[i].x); 36 for(int j=1;j<=4;++j)scanf("%lf",&q[i].z[j]); 37 } 38 q[0].x=0;q[++n].x=10; 39 for(int i=1;i<=4;++i)q[0].z[i]=q[n].z[i]=5; 40 for(int i=1;i<=n*4+4;++i)for(int j=1;j<=n*4+4;++j)e[i][j]=1e9; 41 42 for(int i=0;i<=n;++i) 43 for(int j=i+1;j<=n;++j) 44 for(int a=1;a<=4;++a) 45 for(int b=1;b<=4;++b) 46 add(i,j,a,b); 47 for(int k=1;k<=n*4+4;++k) 48 for(int i=1;i<=n*4+4;++i) 49 for(int j=1;j<=n*4+4;++j) 50 e[i][j]=min(e[i][j],e[i][k]+e[k][j]); 51 printf("%.2lf",e[1][n*4+1]); 52 return 0; 53 }