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

首先路径的每条线段一定是端点之间的连线。证明?这是个坑...反正我是随便画了一下图然后就写了..

然后re是什么节奏?我记得我开够了啊...然后再开大点才a...好囧啊.

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <set>
  9. #include <map>
  10. using namespace std;
  11. typedef long long ll;
  12. #define rep(i, n) for(int i=0; i<(n); ++i)
  13. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  14. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  15. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  16. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  17. #define CC(i,a) memset(i,a,sizeof(i))
  18. #define read(a) a=getint()
  19. #define print(a) printf("%d", a)
  20. #define dbg(x) cout << (#x) << " = " << (x) << endl
  21. #define error(x) (!(x)?puts("error"):0)
  22. #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
  23. inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
  24.  
  25. const double eps=1e-6;
  26. int dcmp(double x) { return abs(x)<eps?0:(x<0?-1:1); }
  27. struct ipoint { double x, y; };
  28. double icross(ipoint &a, ipoint &b, ipoint &c) {
  29. static double x1, x2, y1, y2;
  30. x1=a.x-c.x; y1=a.y-c.y;
  31. x2=b.x-c.x; y2=b.y-c.y;
  32. return x1*y2-x2*y1;
  33. }
  34. int ijiao(ipoint &p1, ipoint &p2, ipoint &q1, ipoint &q2) {
  35. return (dcmp(icross(p1, q1, q2))^dcmp(icross(p2, q1, q2)))==-2 &&
  36. (dcmp(icross(q1, p1, p2))^dcmp(icross(q2, p1, p2)))==-2;
  37. }
  38.  
  39. const int N=1000;
  40. struct dat { int next, to; double w; }e[N<<2];
  41. int ihead[N], cnt;
  42. void add(int u, int v, double w) {
  43. e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
  44. }
  45. double spfa(int s, int t, int n) {
  46. static double d[N];
  47. static int q[N], front, tail, u, v;
  48. static bool vis[N];
  49. front=tail=0;
  50. for1(i, 0, n) vis[i]=0, d[i]=1e99;
  51. d[s]=0; q[tail++]=s; vis[s]=1;
  52. while(front!=tail) {
  53. u=q[front++]; if(front==N) front=0; vis[u]=0;
  54. rdm(u, i) if(d[v=e[i].to]+eps>d[u]+e[i].w) {
  55. d[v]=d[u]+e[i].w;
  56. if(!vis[v]) {
  57. vis[v]=1;
  58. if(d[v]<d[q[front]]+eps) {
  59. --front; if(front<0) front+=N;
  60. q[front]=v;
  61. }
  62. else { q[tail++]=v; if(tail==N) tail=0; }
  63. }
  64. }
  65. }
  66. return d[t];
  67. }
  68.  
  69. ipoint p[N], line[N*3][2];
  70. int n, pn, ln;
  71.  
  72. bool check(ipoint &x, ipoint &y) {
  73. for1(i, 1, ln) if(ijiao(x, y, line[i][0], line[i][1])) return false;
  74. return true;
  75. }
  76. double sqr(double x) { return x*x; }
  77. double dis(ipoint &x, ipoint &y) { return sqrt(sqr(x.x-y.x)+sqr(x.y-y.y)); }
  78.  
  79. int main() {
  80. while(read(n), n!=-1) {
  81. ln=0; pn=0;
  82. ++pn; p[pn].x=0; p[pn].y=5;
  83. ++pn; p[pn].x=10; p[pn].y=5;
  84. static double rx, ry[4];
  85. while(n--) {
  86. scanf("%lf", &rx);
  87. rep(k, 4) scanf("%lf", &ry[k]);
  88. ++ln; line[ln][0]=(ipoint){rx, 0}; line[ln][1]=(ipoint){rx, ry[0]};
  89. ++ln; line[ln][0]=(ipoint){rx, ry[1]}; line[ln][1]=(ipoint){rx, ry[2]};
  90. ++ln; line[ln][0]=(ipoint){rx, ry[3]}; line[ln][1]=(ipoint){rx, 10};
  91. rep(k, 4) ++pn, p[pn].x=rx, p[pn].y=ry[k];
  92. }
  93. for1(i, 1, pn) for1(j, 1, pn) if(i!=j && check(p[i], p[j])) add(i, j, dis(p[i], p[j]));
  94. printf("%.2f\n", spfa(1, 2, pn));
  95.  
  96. memset(ihead, 0, sizeof(int)*(pn+1));
  97. cnt=0;
  98. }
  99. return 0;
  100. }

  


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

【POJ】1556 The Doors(计算几何基础+spfa)的更多相关文章

  1. POJ 1556 - The Doors 线段相交不含端点

    POJ 1556 - The Doors题意:    在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少.    分析:        要么直达,要么 ...

  2. POJ 1556 The Doors 线段交 dijkstra

    LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...

  3. POJ 1556 - The Doors - [平面几何+建图spfa最短路]

    题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...

  4. poj 1556 The Doors

    The Doors Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u   Java ...

  5. POJ 1556 The Doors【最短路+线段相交】

    思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...

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

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

  7. POJ 1556 The Doors(线段交+最短路)

    The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5210   Accepted: 2124 Descrip ...

  8. poj 1556 The Doors(线段相交,最短路)

      The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7430   Accepted: 2915 Descr ...

  9. POJ 1556 The Doors 线段判交+Dijkstra

    The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6734   Accepted: 2670 Descrip ...

随机推荐

  1. django-cms 代码研究(三)插件(plugs in)

    插件(plugs in) djangocms支持的插件有: http://docs.django-cms.org/en/latest/basic_reference/plugin_reference. ...

  2. 双参数Bellman-ford带队列优化类似于背包问题的递推

    为方便起见,将Bellman-ford队列优化称为SPFA,= = 抓住 ZMF (ZMF.pas/c/cpp) 题目描述 话说这又是一个伸手不见五指的夜晚,为了机房的电子竞技事业永远孜孜不倦的 ZM ...

  3. wget批量下载

    wget -i download.txt 这样就会把download.txt里面列出的每个URL都下载下来. wget -c http://the.url.of/incomplete/file 使用断 ...

  4. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  5. 12.从上往下遍历二元树[LevelOrderOfBinaryTree]

    [题目] 输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印. 例如输入 8    /  \   6    10  /\     /\ 5  7   9  11 输出8    ...

  6. js将map转成数组

    //根据资源的ID去查找 this.classArray = []; for(var c in this.comboData.classId){ this.classArray.push({ text ...

  7. HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  8. osgEarth例子

    #include <osgViewer/Viewer>#include <osgViewer/ViewerEventHandlers>#include <osgGA/St ...

  9. [MAC] mac系统如何截图

    mac自带截图工具,因此不需要安装任何第三方软件,便可以实现屏幕截图,截图的方法有若干种,下面介绍最简单的方法:通过快捷键进行截图: 全屏截图: 同时按住键盘左下方的  command   和   s ...

  10. Timer&TimerTask原理分析

    转载地址,请珍惜作者的劳动成果,转载请注明出处:http://www.open-open.com/lib/view/open1337176725619.html 如果你使用Java语言进行开发,对于定 ...