题意: 给一些圆,要求从第一个圆的圆心走到最后一个圆的圆心,中间路径必须在某个圆内,求最短路径的长度。

解法: 易知要保持在圆内且路径最短,走两圆相交的点能使路径尽量短,所以我们找出所有的两圆相交的点,再加上起点和终点,放到一个容器中,去重后,判断每两点之间的线段是否都在圆内,如果是则建边,建完所有的边后跑一个SPFA即可得出最短路。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <queue>
  9. #define Mod 1000000007
  10. #define eps 1e-8
  11. using namespace std;
  12. #define N 100017
  13.  
  14. struct Point{
  15. double x,y;
  16. Point(double x=, double y=):x(x),y(y) {}
  17. };
  18. typedef Point Vector;
  19. struct Circle{
  20. Point c;
  21. double r;
  22. Circle(){}
  23. Circle(Point c,double r):c(c),r(r) {}
  24. Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
  25. void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
  26. };
  27. struct Line{
  28. Point p;
  29. Vector v;
  30. double ang;
  31. Line(){}
  32. Line(Point p, Vector v):p(p),v(v) { ang = atan2(v.y,v.x); }
  33. Point point(double t) { return Point(p.x + t*v.x, p.y + t*v.y); }
  34. bool operator < (const Line &L)const { return ang < L.ang; }
  35. };
  36. int dcmp(double x) {
  37. if(x < -eps) return -;
  38. if(x > eps) return ;
  39. return ;
  40. }
  41. template <class T> T sqr(T x) { return x * x;}
  42. Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
  43. Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
  44. Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
  45. Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
  46. bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
  47. bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
  48. bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
  49. bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
  50. double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
  51. double Length(Vector A) { return sqrt(Dot(A, A)); }
  52. double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
  53. double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
  54. Vector VectorUnit(Vector x){ return x / Length(x);}
  55. Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
  56. double angle(Vector v) { return atan2(v.y, v.x); }
  57.  
  58. bool OnSegment(Point P, Point A, Point B) {
  59. return dcmp(Cross(A-P,B-P)) == && dcmp(Dot(A-P,B-P)) < ;
  60. }
  61. bool InCircle(Point x, Circle c) { return dcmp(c.r - Length(c.c-x)) > ; } //not in border
  62. double DistanceToSeg(Point P, Point A, Point B)
  63. {
  64. if(A == B) return Length(P-A);
  65. Vector v1 = B-A, v2 = P-A, v3 = P-B;
  66. if(dcmp(Dot(v1, v2)) < ) return Length(v2);
  67. if(dcmp(Dot(v1, v3)) > ) return Length(v3);
  68. return fabs(Cross(v1, v2)) / Length(v1);
  69. }
  70. Point GetLineIntersection(Line A, Line B){
  71. Vector u = A.p - B.p;
  72. double t = Cross(B.v, u) / Cross(A.v, B.v);
  73. return A.p + A.v*t;
  74. }
  75. int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol) //return 交点个数
  76. {
  77. double d = Length(C1.c - C2.c);
  78. if(dcmp(d) == ){
  79. if(dcmp(C1.r - C2.r) == ) return -; //两圆重合
  80. return ;
  81. }
  82. if(dcmp(C1.r + C2.r - d) < ) return ;
  83. if(dcmp(fabs(C1.r - C2.r) - d) > ) return ;
  84.  
  85. double a = angle(C2.c - C1.c); //向量C1C2的极角
  86. double da = acos((sqr(C1.r) + sqr(d) - sqr(C2.r)) / (*C1.r*d)); //C1C2到C1P1的极角
  87.  
  88. Point p1 = C1.point(a-da), p2 = C1.point(a+da);
  89. sol.push_back(p1);
  90. if(p1 == p2) return ;
  91. sol.push_back(p2);
  92. return ;
  93. }
  94. int GetSegCircleIntersection(Line L, Circle C, Point* sol)
  95. {
  96. Vector Noml = Normal(L.v);
  97. Line PL = Line(C.c, Noml);
  98. Point IP = GetLineIntersection(PL, L); //弦的中点
  99. double Dis = Length(IP - C.c);
  100. if(dcmp(Dis-C.r) > ) return ; //在圆外
  101. Vector HalfChord = VectorUnit(L.v)*sqrt(sqr(C.r)-sqr(Dis));
  102. int ind = ;
  103. sol[ind] = IP + HalfChord;
  104. if(OnSegment(sol[ind],L.p,L.point())) ind++;
  105. sol[ind] = IP - HalfChord;
  106. if(OnSegment(sol[ind],L.p,L.point())) ind++;
  107. return ind;
  108. }
  109.  
  110. //data segment
  111. vector<Point> sol;
  112. Circle C[];
  113. double dis[];
  114. vector<pair<int,double> > G[];
  115. int n,vis[],S,E;
  116. //data ends
  117.  
  118. bool CheckSegInCircle(Point A, Point B){
  119. int i,j;
  120. vector<Point> now;
  121. now.push_back(A), now.push_back(B);
  122. Point inter[];
  123. for(i=;i<=n;i++) {
  124. int m = GetSegCircleIntersection(Line(A,B-A),C[i],inter);
  125. for(j=;j<=m;j++) now.push_back(inter[j-]);
  126. }
  127. sort(now.begin(), now.end());
  128. int sz = now.size();
  129. for(i=;i<sz-;i++) {
  130. Point mid = (now[i] + now[i+])/2.0;
  131. if(mid == now[i]) continue;
  132. for(j=;j<=n;j++)
  133. if(InCircle(mid,C[j]))
  134. break;
  135. if(j == n+) return false;
  136. }
  137. return true;
  138. }
  139.  
  140. void SPFA(int n)
  141. {
  142. for(int i=;i<=n;i++) dis[i] = Mod;
  143. memset(vis,,sizeof(vis));
  144. dis[S] = , vis[S] = ;
  145. queue<int> q;
  146. q.push(S);
  147. while(!q.empty())
  148. {
  149. int u = q.front();
  150. q.pop(); vis[u] = ;
  151. for(int i=;i<G[u].size();i++)
  152. {
  153. int v = G[u][i].first;
  154. double w = G[u][i].second;
  155. if(dis[v] > dis[u] + w)
  156. {
  157. dis[v] = dis[u] + w;
  158. if(!vis[v]) vis[v] = , q.push(v);
  159. }
  160. }
  161. }
  162. }
  163.  
  164. int main()
  165. {
  166. int t,cs = ,i,j;
  167. scanf("%d",&t);
  168. while(t--)
  169. {
  170. scanf("%d",&n);
  171. sol.clear();
  172. for(i=;i<=;i++) G[i].clear();
  173. C[].input(), sol.push_back(C[].c);
  174. for(i=;i<n;i++) C[i].input();
  175. C[n].input(), sol.push_back(C[n].c);
  176. for(i=;i<=n;i++)
  177. for(j=i+;j<=n;j++)
  178. GetCircleCircleIntersection(C[i],C[j],sol);
  179. sort(sol.begin(), sol.end());
  180. int ind = unique(sol.begin(), sol.end()) - sol.begin();
  181. for(i=;i<ind;i++)
  182. {
  183. for(j=i+;j<ind;j++)
  184. {
  185. if(CheckSegInCircle(sol[i],sol[j]))
  186. {
  187. G[i].push_back(make_pair(j,Length(sol[i]-sol[j])));
  188. G[j].push_back(make_pair(i,Length(sol[i]-sol[j])));
  189. }
  190. }
  191. if(sol[i] == C[].c) S = i;
  192. if(sol[i] == C[n].c) E = i;
  193. }
  194. SPFA(ind);
  195. printf("Case %d: ",cs++);
  196. if(dcmp(dis[E]-Mod) >= ) puts("No such path.");
  197. else printf("%.4f\n",dis[E]);
  198. }
  199. return ;
  200. }

HDU 4063 Aircraft --几何,最短路的更多相关文章

  1. hdu 4063 Aircraft(计算几何+最短路)

    不说了...说多了都是泪...从昨天下午一直wa到现在,直到刚刚才让人帮我找到所谓的“bug”,其实也算不上bug... 这个题的思路就是:找出平面上的所有点:所有圆的交点以及所有圆的圆心.然后依次判 ...

  2. HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4063 Description You are playing a flying game. In th ...

  3. hdu 4063 Aircraft (Geometry + SP)

    Problem - 4063 几何加简单最短路. 题意是给出若干圆的圆心以及半径,求出从给出的起点到终点的最短路径的长度,可以移动的区域是圆覆盖到的任意一个位置. 做法是这样的,对圆两两求交点,用这些 ...

  4. HDU 4063 线段与圆相交+最短路

    Aircraft Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. hdu 1245 Saving James Bond 策画几何+最短路 最短路求步数最少的路径

    #include<stdio.h> #include<string.h> #include<math.h> #define inf 0x3fffffff #defi ...

  6. HDU 5294 Tricks Device 最短路+最大流

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点1到节点n的最短路径, 2.最多能删除 ...

  7. 2017多校第10场 HDU 6181 Two Paths 次短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6181 题意:给一个图,求出次短路. 解法:我之前的模板不能解决这种图,就是最短路和次短路相等的情况,证 ...

  8. HDU 5889 Barricade(最短路+最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意: 给出一个图,帝国将军位于1处,敌军位于n处,敌军会选择最短路到达1点.现在帝国将军要在路径上放置障 ...

  9. hdu 5521 Meeting(最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题意:有1-n共n个点,给出m个块(完全图),并知道块内各点之间互相到达花费时间均为ti.已知两 ...

随机推荐

  1. SQL数据库基础(七)

    主键 数据库主键是指表中一个列或列的组合,其值能唯一地标识表中的每一行.这样的一列或多列称为表的主键,通过它可强制表的实体完整性.当创建或更改表时可通过定义 PRIMARY KEY约束来创建主键.一个 ...

  2. play HTTP路由 http://play-framework.herokuapp.com/zh/routes#syntax

    HTTP路由 HTTP路由(译者注:Play的路径映射机制)组件负责将HTTP请求交给对应的action(一个控制器Controller的公共静态方法)处理. 对于MVC框架来说,一个HTTP请求可以 ...

  3. TCP中close和shutdown之间的区别

    该图片截取自<<IP高效编程-改善网络编程的44个技巧>>,第17个技巧.  如果想验证可以写个简单的网络程序,分别用close和shutdown来断开连接,然后用tcpdum ...

  4. SAP ALV标准范例程序介绍

    下面介绍几个学习ALV的SAP自带标准程序实例 1. BALVSD06 : Output flights (simple version + save)这是该系列范例最简单的一个,建议以此入门.使用的 ...

  5. SharePoint Iframe 报错“此内容不能显示在一个框架中”

    问题描述 我们SharePoint站点用Excel Service发布的Excel,需要Iframe到其他系统中,但是,Iframe的时候发现报错“此内容不能显示在一个框架中”. 后来,尝试在其他系统 ...

  6. apache-virtual host

    NameVirtualHost xxx.xxx.xxx.xxx:80<VirtualHost xxx.xxx.xxx.xxx:80>        ServerName xxx.xxx.x ...

  7. Xcode 文件删除拷贝 出现的问题

    当删除一个组的时候,不管是下面的两个选择,是彻底删除还是不彻底: 然后又要往工程里拷贝进去  同名  文件组,最好是选择Creat groups (因为创建groups就不会有import的时候,还需 ...

  8. windows下vagrant使用及工作环境配置

    环境搭建记录(2014-08-01) 操作系统: Win7旗舰版 Vagrant版本: 1.6 搭建过程 安装vagrant 右键打开安装包按照提示安装即可 安装后会自动把vagrant添加到环境变量 ...

  9. HTTPS连接的前几毫秒发生了什么——Amazon HTTPS案例分析

    转自: http://blog.jobbole.com/48369/ 提示:英文原文写于2009年,当时的Firefox和最新版的Firefox,界面也有很大改动.以下是正文. 花了数小时阅读了如潮的 ...

  10. facebook开源前端UI框架React初探

    最近最火的前端UI框架非React莫属了.赶紧找时间了解一下. 项目地址:http://facebook.github.io/react/ 官方的介绍:A JavaScript library for ...