标签:style blog http io ar color os sp for
题意: 给一些圆,要求从第一个圆的圆心走到最后一个圆的圆心,中间路径必须在某个圆内,求最短路径的长度。
解法: 易知要保持在圆内且路径最短,走两圆相交的点能使路径尽量短,所以我们找出所有的两圆相交的点,再加上起点和终点,放到一个容器中,去重后,判断每两点之间的线段是否都在圆内,如果是则建边,建完所有的边后跑一个SPFA即可得出最短路。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <queue> #define Mod 1000000007 #define eps 1e-8 using namespace std; #define N 100017 struct Point{ double x,y; Point(double x=0, double y=0):x(x),y(y) {} }; typedef Point Vector; struct Circle{ Point c; double r; Circle(){} Circle(Point c,double r):c(c),r(r) {} Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); } void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); } }; struct Line{ Point p; Vector v; double ang; Line(){} Line(Point p, Vector v):p(p),v(v) { ang = atan2(v.y,v.x); } Point point(double t) { return Point(p.x + t*v.x, p.y + t*v.y); } bool operator < (const Line &L)const { return ang < L.ang; } }; int dcmp(double x) { if(x < -eps) return -1; if(x > eps) return 1; return 0; } template <class T> T sqr(T x) { return x * x;} Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); } Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); } bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; } bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; } bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } Vector VectorUnit(Vector x){ return x / Length(x);} Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);} double angle(Vector v) { return atan2(v.y, v.x); } bool OnSegment(Point P, Point A, Point B) { return dcmp(Cross(A-P,B-P)) == 0 && dcmp(Dot(A-P,B-P)) < 0; } bool InCircle(Point x, Circle c) { return dcmp(c.r - Length(c.c-x)) > 0; } //not in border double DistanceToSeg(Point P, Point A, Point B) { if(A == B) return Length(P-A); Vector v1 = B-A, v2 = P-A, v3 = P-B; if(dcmp(Dot(v1, v2)) < 0) return Length(v2); if(dcmp(Dot(v1, v3)) > 0) return Length(v3); return fabs(Cross(v1, v2)) / Length(v1); } Point GetLineIntersection(Line A, Line B){ Vector u = A.p - B.p; double t = Cross(B.v, u) / Cross(A.v, B.v); return A.p + A.v*t; } int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol) //return 交点个数 { double d = Length(C1.c - C2.c); if(dcmp(d) == 0){ if(dcmp(C1.r - C2.r) == 0) return -1; //两圆重合 return 0; } if(dcmp(C1.r + C2.r - d) < 0) return 0; if(dcmp(fabs(C1.r - C2.r) - d) > 0) return 0; double a = angle(C2.c - C1.c); //向量C1C2的极角 double da = acos((sqr(C1.r) + sqr(d) - sqr(C2.r)) / (2*C1.r*d)); //C1C2到C1P1的极角 Point p1 = C1.point(a-da), p2 = C1.point(a+da); sol.push_back(p1); if(p1 == p2) return 1; sol.push_back(p2); return 2; } int GetSegCircleIntersection(Line L, Circle C, Point* sol) { Vector Noml = Normal(L.v); Line PL = Line(C.c, Noml); Point IP = GetLineIntersection(PL, L); //弦的中点 double Dis = Length(IP - C.c); if(dcmp(Dis-C.r) > 0) return 0; //在圆外 Vector HalfChord = VectorUnit(L.v)*sqrt(sqr(C.r)-sqr(Dis)); int ind = 0; sol[ind] = IP + HalfChord; if(OnSegment(sol[ind],L.p,L.point(1))) ind++; sol[ind] = IP - HalfChord; if(OnSegment(sol[ind],L.p,L.point(1))) ind++; return ind; } //data segment vector<Point> sol; Circle C[33]; double dis[2510]; vector<pair<int,double> > G[2510]; int n,vis[2510],S,E; //data ends bool CheckSegInCircle(Point A, Point B){ int i,j; vector<Point> now; now.push_back(A), now.push_back(B); Point inter[2]; for(i=1;i<=n;i++) { int m = GetSegCircleIntersection(Line(A,B-A),C[i],inter); for(j=1;j<=m;j++) now.push_back(inter[j-1]); } sort(now.begin(), now.end()); int sz = now.size(); for(i=0;i<sz-1;i++) { Point mid = (now[i] + now[i+1])/2.0; if(mid == now[i]) continue; for(j=1;j<=n;j++) if(InCircle(mid,C[j])) break; if(j == n+1) return false; } return true; } void SPFA(int n) { for(int i=0;i<=n;i++) dis[i] = Mod; memset(vis,0,sizeof(vis)); dis[S] = 0, vis[S] = 1; queue<int> q; q.push(S); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for(int i=0;i<G[u].size();i++) { int v = G[u][i].first; double w = G[u][i].second; if(dis[v] > dis[u] + w) { dis[v] = dis[u] + w; if(!vis[v]) vis[v] = 1, q.push(v); } } } } int main() { int t,cs = 1,i,j; scanf("%d",&t); while(t--) { scanf("%d",&n); sol.clear(); for(i=0;i<=2500;i++) G[i].clear(); C[1].input(), sol.push_back(C[1].c); for(i=2;i<n;i++) C[i].input(); C[n].input(), sol.push_back(C[n].c); for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) GetCircleCircleIntersection(C[i],C[j],sol); sort(sol.begin(), sol.end()); int ind = unique(sol.begin(), sol.end()) - sol.begin(); for(i=0;i<ind;i++) { for(j=i+1;j<ind;j++) { if(CheckSegInCircle(sol[i],sol[j])) { G[i].push_back(make_pair(j,Length(sol[i]-sol[j]))); G[j].push_back(make_pair(i,Length(sol[i]-sol[j]))); } } if(sol[i] == C[1].c) S = i; if(sol[i] == C[n].c) E = i; } SPFA(ind); printf("Case %d: ",cs++); if(dcmp(dis[E]-Mod) >= 0) puts("No such path."); else printf("%.4f\n",dis[E]); } return 0; }
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/whatbeg/p/4107746.html