[最短路]luogu P1354 房间最短路问题
题面
https://www.luogu.com.cn/problem/P1354
分析
傻逼题,评级虚高
判断墙点之间是否有墙阻隔,没有连个边,直接跑folyd即可
代码
#include <iostream> #include <cstdio> #include <cmath> using namespace std; const double Inf=21.0; const int N=22; struct Wall { double x,y[4]; }p[N]; int n; double g[N][4][N][4]; inline double Dist(double x1,double y1,double x2,double y2) {return sqrt(pow(x1-x2,2)+pow(y1-y2,2));} inline double Slope(double x1,double y1,double x2,double y2) {return (y2-y1)/(x2-x1);} inline double Intercept(double x,double y,double k) {return y-k*x;} inline double Function(double x,double k,double b) {return x*k+b;} bool Check(int u,int v,double y1,double y2) { if (u>=v) return 0; double k=Slope(p[u].x,y1,p[v].x,y2),b=Intercept(p[u].x,y1,k); for (int i=u+1;i<=v;i++) { double f=Function(p[i].x,k,b); if (f<p[i].y[0]||p[i].y[1]<f&&f<p[i].y[2]||p[i].y[3]<f) return 0; } return 1; } int main() { scanf("%d",&n);n++; for (int i=1;i<n;i++) scanf("%lf%lf%lf%lf%lf",&p[i].x,&p[i].y[0],&p[i].y[1],&p[i].y[2],&p[i].y[3]); p[0].x=0;p[n].x=10;p[0].y[0]=p[0].y[1]=p[0].y[2]=p[0].y[3]=p[n].y[0]=p[n].y[1]=p[n].y[2]=p[n].y[3]=5; for (int i=0;i<=n;i++) for (int j=0;j<4;j++) for (int k=0;k<=n;k++) for (int l=0;l<4;l++) if (Check(i,k,p[i].y[j],p[k].y[l])) g[i][j][k][l]=Dist(p[i].x,p[i].y[j],p[k].x,p[k].y[l]); else g[i][j][k][l]=Inf; for (int m=0;m<=n;m++) for (int s=0;s<4;s++) for (int i=0;i<=n;i++) for (int j=0;j<4;j++) for (int k=0;k<=n;k++) for (int l=0;l<4;l++) g[i][j][k][l]=min(g[i][j][k][l],g[i][j][m][s]+g[m][s][k][l]); printf("%.2lf",g[0][0][n][0]); }
在日渐沉没的世界里,我发现了你。