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

The Doors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6120   Accepted: 2455

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


開始的时候真的是二逼了,

1、推断相交的函数写错了,我竟然推断的是是不是跟源点和终点的直线相交。。。二逼啊,,,

2、然后改了之后还wa,由于推断里少了个!,,,,没取反,,,

3、极限的点,比方每道墙的最上沿和最下沿,这两个点不可达,就是说从源头到终点不能经过这两个点,開始的时候没排除,尽管那样的话也能AC,还是题目数据太弱了啊

我自己写的推断直线相交的模板:

/*==========================================================*\
|| 推断点在直线上或直线相交
1、函数值为0,表示在直线上;
2、test(a,b,t1)*test(a,b,t2)<0表示直线ab和直线t1t2相交
\*==========================================================*/
double test(Point a,Point b, Point t)
{
return (b.y-a.y)*(t.x-b.x)-(b.x-a.x)*(t.y-b.y);
}

思路还是比較顺的,就是最短路+推断直线相交

贴代码:

#include<cstdio>
#include<cstring>
#include <string>
#include <map>
#include <iostream>
#include <cmath>
using namespace std;
#define INF 10000
const double eps=1e-6; const int MAXN = 1011;
#define Max(a,b) (a)>(b)?(a):(b)
int cntp;
int wn;
struct Point{
Point(double x=0,double y=0):x(x),y(y){}
double x,y;
int id;
}p[MAXN];
struct Wall{
double s1,e1;
double s2,e2;
double s3;
}w[20];//=0~~=wn
double e[MAXN][MAXN],dist[MAXN]; double dis(Point a, Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} void init()
{
cntp=1;
for(int i=0;i<=MAXN;i++)
for(int j=0;j<=MAXN;j++)
{
if(i == j)e[i][j]=0;
else e[i][j]=INF;
}
p[0].x=0,p[0].y=5,p[0].id=0;
for(int i=0;i<=MAXN;i++)dist[i]=INF;
} double test(Point a,Point b, Point t)
{
return (b.y-a.y)*(t.x-b.x)-(b.x-a.x)*(t.y-b.y);
} bool Judge(Point a, Point b)
{
if(a.id>b.id)
{
Point t=a;
a=b;
b=t;
}
//int flag=1;
if(a.id>0)
if(a.y -0.0 <=eps||10.0-a.y <=eps)
return 0;
if(b.id<cntp-1)
if(b.y-0.0<=eps || 10.0-b.y<=eps)
return 0; for(int i=a.id+1;i<b.id;i++)
{
Point p1(w[i].s1,w[i].e1),p2(w[i].s1,w[i].s2),p3(w[i].s1,w[i].e2),p4(w[i].s1,w[i].s3); if(!(
test(a,b,p1)*test(a,b,p2)<0 ||
test(a,b,p3)*test(a,b,p4)<0)
)return 0;
}
/*for(int i=a.id+1;i<b.id;i++)
{
if(!(
(w[i].e1<5.0&&w[i].s2>5.0)
|| (w[i].e2<5.0&&w[i].s3>5.0)
)
)return 0;
}*/
return 1;
} void Build()
{
for(int i=0;i<cntp;i++)
{
for(int j=i+1;j<cntp;j++)
{
//if(i == j)continue;
if(p[i].id == p[j].id)continue; if(Judge(p[i],p[j]))
{
e[i][j]=min(e[i][j],dis(p[i],p[j]));
}
}
}
} void Bellman(int v0)
{
int n=cntp;
for(int i=0;i<cntp;i++)
{
dist[i]=e[v0][i];
//if(i!=v0 && dist[i]<INF)
}
for(int k=2;k<n;k++)
{
for(int u=0;u<n;u++)
{
if(u!=v0)
{
for(int j=0;j<n;j++)
{
if(e[j][u]!=INF && dist[j]+e[j][u]<dist[u])
{
dist[u]=dist[j]+e[j][u];
}
}
}
}
}
} int main()
{
// freopen("poj1556.txt","r",stdin);
while(~scanf("%d",&wn) && ~wn)
{
init();
for(int i=1;i<=wn;i++)
{
scanf("%lf%lf%lf%lf%lf",&w[i].s1,&w[i].e1,&w[i].s2,&w[i].e2,&w[i].s3);
p[cntp].id=p[cntp+1].id=p[cntp+2].id=p[cntp+3].id=p[cntp+4].id=p[cntp+5].id=i;
p[cntp].x=p[cntp+1].x=p[cntp+2].x=p[cntp+3].x=p[cntp+4].x=p[cntp+5].x=w[i].s1;
p[cntp].y=0.0,p[cntp+1].y=w[i].e1,p[cntp+2].y=w[i].s2,p[cntp+3].y=w[i].e2,p[cntp+4].y=w[i].s3,p[cntp+5].y=10.0;
////////////////
//e[cntp+1][cntp+2]=w[i].s2-w[i].e1;
// e[cntp+3][cntp+4]=w[i].s3-w[i].e2;
cntp+=6;
}
p[cntp].x=10.0,p[cntp].y=5.0,p[cntp].id=++wn;
cntp++;
//if()
Build();
Bellman(0);
printf("%.2lf\n",dist[cntp-1]);
///////////////////
// for(int i=0;i<cntp;i++)
// printf("%d %lf\n",i,dist[i]);
}
return 0;
}

poj 1556 zoj1721 BellmanFord 最短路+推断直线相交的更多相关文章

  1. POJ 1039 Pipe【经典线段与直线相交】

    链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  2. [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 Descr ...

  3. 最短路+线段交 POJ 1556 好题

    // 最短路+线段交 POJ 1556 好题 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终 ...

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

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

  5. POJ 1556 The Doors 线段交 dijkstra

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

  6. 直线相交 POJ 1269

    // 直线相交 POJ 1269 // #include <bits/stdc++.h> #include <iostream> #include <cstdio> ...

  7. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  8. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  9. POJ 1269 Intersecing Lines (直线相交)

    题目: Description We all know that a pair of distinct points on a plane defines a line and that a pair ...

随机推荐

  1. Extjs Web Desktop申请书

    今天我Web Desktop应用基本完成.多语言支持.现有asp,php,jsp版本号. 废话拍了几张照片让大家有一个直观的了解: watermark/2/text/aHR0cDovL2Jsb2cuY ...

  2. BibTex (.bib) 文件的凝视

    1) 将某个參考文献所有去掉,能够去掉前面的『@』 样例 @article{##,    author = {###},    title = {###},    journal = {###},   ...

  3. Java线(一个):线程安全的和不安全

    当我们看JDK API什么时候,总是找一些类描述说:,线程安全或线程安全,例如StringBuilder在,么一句,"将StringBuilder 的实例用于多个线程是不安全的.假设须要这种 ...

  4. Sphinx/Coreseek 4.1 跑 buildconf.sh 一个错误,无法生成configure档

    安装前 coorseek 什么时候,遇到一些错误.该官方网站无法看到的解决方案,终于 google 在大牛的博客评论区找到一个解决方案.突然跑到他的膝盖介绍~~ 这里整理是为了方便一些人发现,墙毕竟让 ...

  5. SQL Server中tempdb的管理

    原文:SQL Server中tempdb的管理 资料来自: http://blogs.msdn.com/b/sqlserverstorageengine/archive/tags/tempdb/ ht ...

  6. 巧妙设备MTU的大小,轻松提网速

    MTU是什么? "MTU=最大传输单元 单位:字节" 我们在使用互联网时进行的各种网络操作,都是通过一个又一个"数据包"传输来实现的.而MTU指定了网络中可数据 ...

  7. Java得到年在一个季度的错误的第一天

    1.错误叙述性说明 Exception in thread "main" java.lang.IllegalArgumentException: Cannot format giv ...

  8. 024找到二维阵列(keep it up)

    剑指offer在标题中:http://ac.jobdu.com/problem.php? pid=1384 题目描写叙述: 在一个二维数组中,每一行都依照从左到右递增的顺序排序.每一列都依照从上到下递 ...

  9. Android新浪微博client(七)——ListView图片异步加载、高速缓存

    原文出自:方杰|p=193" style="color:rgb(202,0,0); text-decoration:none; font-size:14px; font-famil ...

  10. SSH-Struts(三)—ActionForm

    ActionForm ActionForm用于封装用户的请求数据.即用户在页面上输入的数据.它充当了一个运输工具的角色.为了能够立体的理解ActionFrom.以下从两个角度看看:     从封装的数 ...