【學習筆記】單源最短路徑
阿新 • • 發佈:2021-08-03
純板子。
隨做題也不一定補充。
int js(double x) { if(Abs(x) < eps) return 0; return x > 0 ? 1 : -1; } struct point { double x,y; point(){} point(double xx,double yy){ x = xx; y = yy; } void Get(){scanf("%lf %lf",&x,&y);} void print(){printf("%.2f %.2f\n",x,y);} point operator + (const point &C){return point(x+C.x,y+C.y);} point operator - (const point &C){return point(x-C.x,y-C.y);} point operator * (const double C){return point(x*C,y*C);} bool operator < (const point &px)const{ if(x != px.x) return x < px.x; return y < px.y; } bool operator == (const point &C)const{ return (!js(x-C.x)) && (!js(y-C.y)); } point rot90(){return point(-y,x);} }p[MAXN]; double cross(point A,point B){return A.x*B.y-A.y*B.x;} double dot(point A,point B){return A.x*B.x+A.y*B.y;} double len(point A){return sqrt(A.x*A.x+A.y*A.y);} bool OnSegment(point A,point B1,point B2) { point c1 = B1 - A,c2 = B2 - A;//向量 if(js(cross(c1,c2))) return 0;//判斷向量共線 if(js(c1.x*c2.x) <= 0 && js(c1.y*c2.y) <= 0) return 1;//判斷向量異向 return 0; } bool check(point A1,point A2,point B1,point B2) { if(OnSegment(A1,B1,B2) || OnSegment(A2,B1,B2)) return 1; if(OnSegment(B1,A1,A2) || OnSegment(B2,A1,A2)) return 1; point C1 = A2-A1,C2 = B2-B1; if(!js(cross(C1,C2))) return 0; double c1 = cross(A2-A1,B1-A1),c2 = cross(A2-A1,B2-A1), c3 = cross(B2-B1,A1-B1),c4 = cross(B2-B1,A2-B1); return (js(c1) * js(c2) <= 0) && (js(c3) * js(c4) <= 0); } point inter(point A1, point A2, point B1, point B2)//intersection { point c1 = A2 - A1, c2 = B2 - B1; double t = cross(c2,(A1 - B1)) / cross(c1,c2); return c1*t+A1; } point Cir(point A,point B,point C){return inter((A+B)/2,(B-A).rot90(),(A+C)/2,(C-A).rot90());}//三點求圓心(不共線)