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

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

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

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
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; } const double eps=1e-6;
int dcmp(double x) { return abs(x)<eps?0:(x<0?-1:1); }
struct ipoint { double x, y; };
double icross(ipoint &a, ipoint &b, ipoint &c) {
static double x1, x2, y1, y2;
x1=a.x-c.x; y1=a.y-c.y;
x2=b.x-c.x; y2=b.y-c.y;
return x1*y2-x2*y1;
}
int ijiao(ipoint &p1, ipoint &p2, ipoint &q1, ipoint &q2) {
return (dcmp(icross(p1, q1, q2))^dcmp(icross(p2, q1, q2)))==-2 &&
(dcmp(icross(q1, p1, p2))^dcmp(icross(q2, p1, p2)))==-2;
} const int N=1000;
struct dat { int next, to; double w; }e[N<<2];
int ihead[N], cnt;
void add(int u, int v, double w) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
}
double spfa(int s, int t, int n) {
static double d[N];
static int q[N], front, tail, u, v;
static bool vis[N];
front=tail=0;
for1(i, 0, n) vis[i]=0, d[i]=1e99;
d[s]=0; q[tail++]=s; vis[s]=1;
while(front!=tail) {
u=q[front++]; if(front==N) front=0; vis[u]=0;
rdm(u, i) if(d[v=e[i].to]+eps>d[u]+e[i].w) {
d[v]=d[u]+e[i].w;
if(!vis[v]) {
vis[v]=1;
if(d[v]<d[q[front]]+eps) {
--front; if(front<0) front+=N;
q[front]=v;
}
else { q[tail++]=v; if(tail==N) tail=0; }
}
}
}
return d[t];
} ipoint p[N], line[N*3][2];
int n, pn, ln; bool check(ipoint &x, ipoint &y) {
for1(i, 1, ln) if(ijiao(x, y, line[i][0], line[i][1])) return false;
return true;
}
double sqr(double x) { return x*x; }
double dis(ipoint &x, ipoint &y) { return sqrt(sqr(x.x-y.x)+sqr(x.y-y.y)); } int main() {
while(read(n), n!=-1) {
ln=0; pn=0;
++pn; p[pn].x=0; p[pn].y=5;
++pn; p[pn].x=10; p[pn].y=5;
static double rx, ry[4];
while(n--) {
scanf("%lf", &rx);
rep(k, 4) scanf("%lf", &ry[k]);
++ln; line[ln][0]=(ipoint){rx, 0}; line[ln][1]=(ipoint){rx, ry[0]};
++ln; line[ln][0]=(ipoint){rx, ry[1]}; line[ln][1]=(ipoint){rx, ry[2]};
++ln; line[ln][0]=(ipoint){rx, ry[3]}; line[ln][1]=(ipoint){rx, 10};
rep(k, 4) ++pn, p[pn].x=rx, p[pn].y=ry[k];
}
for1(i, 1, pn) for1(j, 1, pn) if(i!=j && check(p[i], p[j])) add(i, j, dis(p[i], p[j]));
printf("%.2f\n", spfa(1, 2, pn)); memset(ihead, 0, sizeof(int)*(pn+1));
cnt=0;
}
return 0;
}

  


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
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1

Sample Output

10.00
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. [ruby on rails] 跟我学之(4)路由映射

    前面<[ruby on rails] 跟我学之Hello World>提到,路由对应的文件是 config/routes.rb 实际上我们只是添加了一句代码: resources :pos ...

  2. 分享一个强大的采集类,还可以模拟php多进程

    做采集的时候,可以使用file_get_contents()去获取网页源代码,但是使用file_get_contents采集,速度慢,而且超时时间,不好控制.如果采集的页面不存在,需要等待的时间很长. ...

  3. Unique Paths | & ||

    Unique Paths I A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diag ...

  4. iOS 图片拉伸的解释

    以前对于ios的图片拉伸参数一直不太理解,终于看到一篇好文章,转载一下,原文地址:http://blog.csdn.net/q199109106q/article/details/8615661 主要 ...

  5. iOS 中constraint 不等于约束和低优先级约束使用的简单体会

    看了些文章发现,在使用constraint时,不等于约束往往是和低优先级约束成对使用的,这样才能实现他们的效果. 看看例子 下面是在3.5存屏幕下的效果 图1,竖屏,在满足>=50的前提下,可以 ...

  6. sqlite 使用记录

    2014年8月13日 18:20:52 SQLite中创建自增字段: 简单的回答:一个声明为 INTEGER PRIMARY KEY 的字段将自动增加. 从 SQLite 的 2.3.4 版本开始,如 ...

  7. 基因变异(codevs 3194)

    题目描述 Description 小毛终于来到了冥王星,这是一颗已经不属于行星的矮行星,它的表面温度低于-220度.在这里,小毛惊奇的发现,他带来的厌氧菌开始了基因变异,裂变的速度与光照时间(秒)成乘 ...

  8. 一箭双雕打开Genesis

    打开记事本,将如下内容填入,保存时将后缀名改为bat @ ECHO 正在清理垃圾文件...del C:\tmp\*.* /f /q@ ECHO 清理完毕@ ECHO OFF@ ECHO.@ ECHO. ...

  9. sina发现并不会去导入qq使用的

    看问题需要多角度,为之不能实现也是有可能没有完善的.确实是由于短时间发布过多,还是bky好点好像有30S

  10. 多个div 一行显示的处理方式

    1.方式一: 通过div的float属性,定义宽度,然后定义float属性和width的属性,实现多个div在一行显示: 2.方式二: 通过div的display的属性,至少进行2成div的displ ...