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. 【Bootstrap】自己主动去适应PC、平面、手机Bootstrap网格系统

    酒吧格英语作为一门系统"grid systems",也有人翻译成"网络格系统".使用固定格子设计布局,其风格整齐而简洁,在二战结束后人气,流风格之中的一个. 1 ...

  2. Caused by: org.springframework.beans.factory.BeanCreationException

    1.错误原因 2014-7-13 17:36:57 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one J ...

  3. UIButton 文字图片排列

    UIButton缺省值是:图画-文字水平,所以我们并不需要调整. 1.写作-图画 水平显示,以前的文本,图片后再次 [btn setTitleEdgeInsets:UIEdgeInsetsMake(0 ...

  4. Binary Tree Inorder Traversal(转)

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  5. ProgressDialog(三)——代号为中心的屏幕上显示ProgressDialog(ProgressBar)

    MainActivity如下面: package cc.testprogressdialog; import android.os.Bundle; import android.view.Gravit ...

  6. 怎么理解Condition(转)

    在java.util.concurrent包中,有两个很特殊的工具类,Condition和ReentrantLock,使用过的人都知道,ReentrantLock(重入锁)是jdk的concurren ...

  7. 每天收获一点点------Hadoop Eclipse插件的使用

    本文所用软件版本:myeclipe2014    hadoop1.2.1 1.安装Hadoop开发插件 下载hadoop-eclipse-plugin-1.2.1.jar,拷贝到myeclipse根目 ...

  8. 【电视桌面CSWUI】电视桌面(launcher)截图欣赏

    网络播放器是最重要的电视桌面.cswui,我们公司做了一个非常大的人力,物力搞一个电视柜.后来一一介绍,简言之发送屏幕截图.给大家看. watermark/2/text/aHR0cDovL2Jsb2c ...

  9. js面向对象的学习笔记九(BOM 与 DOM 经常使用的属性分析)

    一  BOM物 window 的 相关属性 1. 用户配置的机器配置对象 navigator navigator.userAgent //该属性能够查看用户机器浏览器的配置 "Mozilla ...

  10. seaJs组建库

    seaJs组建库的使用   原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以 ...