The Doors

http://poj.org/problem?id=1556

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10466   Accepted: 3891

Description

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length. 

Input

The input data for the illustrated chamber would appear as follows.


4 2 7 8 9 
7 3 4.5 6 7

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.

Output

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input

  1. 1
  2. 5 4 6 7 8
  3. 2
  4. 4 2 7 8 9
  5. 7 3 4.5 6 7
  6. -1

Sample Output

  1. 10.00
  2. 10.06

Source

有18堵墙!!!因为没看清这个疯狂爆RE

poj上交C++会CE,要自己写hypot函数

  1. double hypot(double x,double y){
  2. return sqrt(x*x+y*y);
  3. }
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<vector>
  6. #include<algorithm>
  7. using namespace std;
  8. const double eps=1e-;
  9. const double INF=1e20;
  10. const double PI=acos(-1.0);
  11. const int maxp=;
  12. int sgn(double x){
  13. if(fabs(x)<eps) return ;
  14. if(x<) return -;
  15. else return ;
  16. }
  17. inline double sqr(double x){return x*x;}
  18. struct Point{
  19. double x,y;
  20. Point(){}
  21. Point(double _x,double _y){
  22. x=_x;
  23. y=_y;
  24. }
  25. void input(){
  26. scanf("%lf %lf",&x,&y);
  27. }
  28. void output(){
  29. printf("%.2f %.2f\n",x,y);
  30. }
  31. bool operator == (const Point &b)const{
  32. return sgn(x-b.x) == && sgn(y-b.y)== ;
  33. }
  34. bool operator < (const Point &b)const{
  35. return sgn(x-b.x)==?sgn(y-b.y)<:x<b.x;
  36. }
  37. Point operator - (const Point &b)const{
  38. return Point(x-b.x,y-b.y);
  39. }
  40. //叉积
  41. double operator ^ (const Point &b)const{
  42. return x*b.y-y*b.x;
  43. }
  44. //点积
  45. double operator * (const Point &b)const{
  46. return x*b.x+y*b.y;
  47. }
  48. //返回长度
  49. double len(){
  50. return hypot(x,y);
  51. }
  52. //返回长度的平方
  53. double len2(){
  54. return x*x+y*y;
  55. }
  56. //返回两点的距离
  57. double distance(Point p){
  58. return hypot(x-p.x,y-p.y);
  59. }
  60. Point operator + (const Point &b)const{
  61. return Point(x+b.x,y+b.y);
  62. }
  63. Point operator * (const double &k)const{
  64. return Point(x*k,y*k);
  65. }
  66. Point operator / (const double &k)const{
  67. return Point(x/k,y/k);
  68. }
  69.  
  70. //计算pa和pb的夹角
  71. //就是求这个点看a,b所成的夹角
  72. ///LightOJ1202
  73. double rad(Point a,Point b){
  74. Point p=*this;
  75. return fabs(atan2(fabs((a-p)^(b-p)),(a-p)*(b-p)));
  76. }
  77. //化为长度为r的向量
  78. Point trunc(double r){
  79. double l=len();
  80. if(!sgn(l)) return *this;
  81. r/=l;
  82. return Point(x*r,y*r);
  83. }
  84. //逆时针转90度
  85. Point rotleft(){
  86. return Point(-y,x);
  87. }
  88. //顺时针转90度
  89. Point rotright(){
  90. return Point(y,-x);
  91. }
  92. //绕着p点逆时针旋转angle
  93. Point rotate(Point p,double angle){
  94. Point v=(*this) -p;
  95. double c=cos(angle),s=sin(angle);
  96. return Point(p.x+v.x*c-v.y*s,p.y+v.x*s+v.y*c);
  97. }
  98. };
  99.  
  100. struct Line{
  101. Point s,e;
  102. Line(){}
  103. Line(Point _s,Point _e){
  104. s=_s;
  105. e=_e;
  106. }
  107. bool operator==(Line v){
  108. return (s==v.s)&&(e==v.e);
  109. }
  110. //根据一个点和倾斜角angle确定直线,0<=angle<pi
  111. Line(Point p,double angle){
  112. s=p;
  113. if(sgn(angle-PI/)==){
  114. e=(s+Point(,));
  115. }
  116. else{
  117. e=(s+Point(,tan(angle)));
  118. }
  119. }
  120. //ax+by+c=0;
  121. Line(double a,double b,double c){
  122. if(sgn(a)==){
  123. s=Point(,-c/b);
  124. e=Point(,-c/b);
  125. }
  126. else if(sgn(b)==){
  127. s=Point(-c/a,);
  128. e=Point(-c/a,);
  129. }
  130. else{
  131. s=Point(,-c/b);
  132. e=Point(,(-c-a)/b);
  133. }
  134. }
  135. void input(){
  136. s.input();
  137. e.input();
  138. }
  139. void adjust(){
  140. if(e<s) swap(s,e);
  141. }
  142. //求线段长度
  143. double length(){
  144. return s.distance(e);
  145. }
  146. //返回直线倾斜角 0<=angle<pi
  147. double angle(){
  148. double k=atan2(e.y-s.y,e.x-s.x);
  149. if(sgn(k)<) k+=PI;
  150. if(sgn(k-PI)==) k-=PI;
  151. return k;
  152. }
  153. //点和直线的关系
  154. //1 在左侧
  155. //2 在右侧
  156. //3 在直线上
  157. int relation(Point p){
  158. int c=sgn((p-s)^(e-s));
  159. if(c<) return ;
  160. else if(c>) return ;
  161. else return ;
  162. }
  163. //点在线段上的判断
  164. bool pointonseg(Point p){
  165. return sgn((p-s)^(e-s))==&&sgn((p-s)*(p-e))<=;
  166. }
  167. //两向量平行(对应直线平行或重合)
  168. bool parallel(Line v){
  169. return sgn((e-s)^(v.e-v.s))==;
  170. }
  171. //两线段相交判断
  172. //2 规范相交
  173. //1 非规范相交
  174. //0 不相交
  175. int segcrossseg(Line v){
  176. int d1=sgn((e-s)^(v.s-s));
  177. int d2=sgn((e-s)^(v.e-s));
  178. int d3=sgn((v.e-v.s)^(s-v.s));
  179. int d4=sgn((v.e-v.s)^(e-v.s));
  180. if((d1^d2)==-&&(d3^d4)==-) return ;
  181. return (d1==&&sgn((v.s-s)*(v.s-e))<=||
  182. d2==&&sgn((v.e-s)*(v.e-e))<=||
  183. d3==&&sgn((s-v.s)*(s-v.e))<=||
  184. d4==&&sgn((e-v.s)*(e-v.e))<=);
  185. }
  186. //直线和线段相交判断
  187. //-*this line -v seg
  188. //2 规范相交
  189. //1 非规范相交
  190. //0 不相交
  191. int linecrossseg(Line v){
  192. int d1=sgn((e-s)^(v.s-s));
  193. int d2=sgn((e-s)^(v.e-s));
  194. if((d1^d2)==-) return ;
  195. return (d1==||d2==);
  196. }
  197. //两直线关系
  198. //0 平行
  199. //1 重合
  200. //2 相交
  201. int linecrossline(Line v){
  202. if((*this).parallel(v))
  203. return v.relation(s)==;
  204. return ;
  205. }
  206. //求两直线的交点
  207. //要保证两直线不平行或重合
  208. Point crosspoint(Line v){
  209. double a1=(v.e-v.s)^(s-v.s);
  210. double a2=(v.e-v.s)^(e-v.s);
  211. return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));
  212. }
  213. //点到直线的距离
  214. double dispointtoline(Point p){
  215. return fabs((p-s)^(e-s))/length();
  216. }
  217. //点到线段的距离
  218. double dispointtoseg(Point p){
  219. if(sgn((p-s)*(e-s))<||sgn((p-e)*(s-e))<)
  220. return min(p.distance(s),p.distance(e));
  221. return dispointtoline(p);
  222. }
  223. //返回线段到线段的距离
  224. //前提是两线段不相交,相交距离就是0了
  225. double dissegtoseg(Line v){
  226. return min(min(dispointtoseg(v.s),dispointtoseg(v.e)),min(v.dispointtoseg(s),v.dispointtoseg(e)));
  227. }
  228. //返回点P在直线上的投影
  229. Point lineprog(Point p){
  230. return s+(((e-s)*((e-s)*(p-s)))/((e-s).len2()));
  231. }
  232. //返回点P关于直线的对称点
  233. Point symmetrypoint(Point p){
  234. Point q=lineprog(p);
  235. return Point(*q.x-p.x,*q.y-p.y);
  236. }
  237. };
  238.  
  239. Line L[];
  240. int n;
  241.  
  242. bool Check(Line a,Line b){
  243. if(sgn((a.s-a.e)^(b.s-a.e))*sgn((a.s-a.e)^(b.e-a.e))>) return false;
  244. if(sgn((b.s-b.e)^(a.s-b.e))*sgn((b.s-b.e)^(a.e-b.e))>) return false;
  245. if(sgn(max(a.s.x,a.e.x)-min(b.s.x,b.e.x))>=&&sgn(max(b.s.x,b.e.x)-min(a.s.x,a.e.x))>=
  246. &&sgn(max(a.s.y,a.e.y)-min(b.s.y,b.e.y))>=&&sgn(max(b.s.y,b.e.y)-min(a.s.y,a.e.y))>=)
  247. return true;
  248. else return false;
  249. }
  250.  
  251. double mp[][];
  252.  
  253. int co;
  254.  
  255. void panduan(Line a,int xx,int yy){
  256. if(a.s.y==||a.s.y==||a.e.y==||a.e.y==) return;
  257. for(int i=;i<co;i++){
  258. if(i!=(xx+)/&&i!=(yy+)/){
  259. if(Check(a,L[i])){
  260. return;
  261. }
  262. }
  263. }
  264. //cout<<xx<<" "<<yy<<" "<<a.length()<<endl;
  265.  
  266. mp[xx][yy]=mp[yy][xx]=a.length();
  267. }
  268.  
  269. int main(){
  270. while(~scanf("%d",&n)){
  271. if(n==-) break;
  272. double x,y1,y2,y3,y4;
  273. co=;
  274. for(int i=;i<;i++){
  275. for(int j=;j<;j++){
  276. mp[i][j]=INF;
  277. }
  278. }
  279. Point s,e;
  280. s.x=,s.y=;
  281. e.x=,e.y=;
  282. //起点为0,终点为co
  283. for(int i=;i<=n;i++){
  284. scanf("%lf %lf %lf %lf %lf",&x,&y1,&y2,&y3,&y4);
  285. L[co].s.x=x,L[co].s.y=,L[co].e.x=x,L[co++].e.y=y1;
  286. L[co].s.x=x,L[co].s.y=y2,L[co].e.x=x,L[co++].e.y=y3;
  287. L[co].s.x=x,L[co].s.y=y4,L[co].e.x=x,L[co++].e.y=;
  288. }
  289.  
  290. Line tmp;
  291. int j;
  292. //不包括起点和终点的建图
  293. for(int i=;i<co;i++){
  294. for(int j=i+;j<co;j++){
  295. tmp.s=L[i].s,tmp.e=L[j].s;
  296. panduan(tmp,(i-)*+,(j-)*+);
  297. tmp.s=L[i].s,tmp.e=L[j].e;
  298. panduan(tmp,(i-)*+,(j-)*+);
  299. tmp.s=L[i].e,tmp.e=L[j].s;
  300. panduan(tmp,(i-)*+,(j-)*+);
  301. tmp.s=L[i].e,tmp.e=L[j].e;
  302. panduan(tmp,(i-)*+,(j-)*+);
  303. }
  304. }
  305. //加上起点和终点
  306. for(int i=;i<co;i++){
  307. tmp.s=s,tmp.e=L[i].s;
  308. panduan(tmp,,(i-)*+);
  309. tmp.s=s,tmp.e=L[i].e;
  310. panduan(tmp,,(i-)*+);
  311. tmp.s=e,tmp.e=L[i].s;
  312. panduan(tmp,(i-)*+,);
  313. tmp.s=e,tmp.e=L[i].e;
  314. panduan(tmp,(i-)*+,);
  315. }
  316. tmp.s=s,tmp.e=e;
  317. panduan(tmp,,);
  318. for(int k=;k<=;k++)
  319. for(int i=;i<=;i++)
  320. for(int j=;j<=;j++)
  321. if(mp[i][j]>mp[i][k]+mp[k][j]+eps)
  322. mp[i][j]=mp[i][k]+mp[k][j];
  323. printf("%.2f\n",mp[][]);
  324. }
  325. return ;
  326. }

The Doors(几何+最短路,好题)的更多相关文章

  1. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  2. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. hdu-3790最短路刷题

    title: hdu-3790最短路刷题 date: 2018-10-20 14:50:31 tags: acm 刷题 categories: ACM-最短路 概述 一道最短路的水题,,,尽量不看以前 ...

  4. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  5. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

  6. POJ 1556 The Doors --几何,最短路

    题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少. 解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短 ...

  7. 2018.07.06 POJ1556 The Doors(最短路)

    The Doors Time Limit: 1000MS Memory Limit: 10000K Description You are to find the length of the shor ...

  8. POJ 4046 Sightseeing 枚举+最短路 好题

    有n个节点的m条无向边的图,节点编号为1~n 然后有点权和边权,给出q个询问,每一个询问给出2点u,v 输出u,v的最短距离 这里的最短距离规定为: u到v的路径的所有边权+u到v路径上最大的一个点权 ...

  9. POJ 1556 The Doors(计算几何+最短路)

    这题就是,处理出没两个点.假设能够到达,就连一条边,推断可不能够到达,利用线段相交去推断就可以.最后求个最短路就可以 代码: #include <cstdio> #include < ...

随机推荐

  1. 超链接中 utm_source, utm_medium 等参数的含义是什么?

    作者:张溪梦 Simon链接:https://www.zhihu.com/question/48724061/answer/122730629来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...

  2. 捷通华声TTS在Aster+中的安装过程

    1)挂载TTS光碟 2)安装如下5个rpm软件包 [asterisk@TTS78:/mnt]$ls *.rpmjTTS-5.0.1.0-3.i386.rpm             VocLib_Xi ...

  3. sklearn.svm.SVC 参数说明

    原文地址:sklearn.svm.SVC 参数说明 ============================== 资源: sklearn官网+DOC 库下载GitHub =============== ...

  4. windows挂载gluseter NFS卷

    windows下挂载gluster提供的NFS卷 服务器端的配置: 首先配置好NFS共享,找一台linux试一下,确保挂载成功.linux客户端执行:mount.nfs 10.33.70.20:tes ...

  5. 现在的 Linux 内核和 Linux 2.6 的内核有多大区别?

    作者:larmbr宇链接:https://www.zhihu.com/question/35484429/answer/62964898来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...

  6. 网络文件系统与 Linux

    网络文件系统 是文件系统之上的一个网络抽象,来允许远程客户端以与本地文件系统类似的方式,来通过网络进行访问.虽然 NFS 不是第一个此类系统,但是它已经发展并演变成 UNIX® 系统中最强大最广泛使用 ...

  7. 如何决定Web应用的线程池大小

    线程池(Thread Pool)在Web应用中线程池的大小决定了在任何一个时间点应用可以处理请求的并发数.如果一个系统收到的请求数超过了线程池的大小,那么超出的请求要么进入等待队列要么被拒绝.请注意, ...

  8. 代码生成器 CodeSmith 的使用(四)

    在上一篇的版本中,我们生成了数据库中的字段,使生成的属性更加简洁,可读性也提高了很多,但都是钍对一个数据库的单个表,如果要将数据库中的所有 的表都生成相应的类,表中的字段也都生成属性,运行一次就可以将 ...

  9. Python——连接操作数据库

    1.安装MySQL驱动程序.下载自动安装包,双击安装即可,非常简单. 2.连接MySQL,下面是Python示例代码. import MySQLdbconn=MySQLdb.connect(host= ...

  10. MVC 4 Razor Design Sample Demo Project

    This is a demo project in MCV 4 razor design which encompases the general design of MVC pattern. The ...