poj 1556 (Dijkstra + Geometry 线段相交)
链接:http://poj.org/problem?id=1556
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6216 | Accepted: 2495 |
Description

Input
2
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
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 ////////////////////////////////////////////////////////////////////
这题处理起来挺难的,要把输入的点存到图里,用Dijkstra求出最短路径,存图的过程是,判断任意两点连成的线,横坐标不能相同,并且如果,横坐标与线上的横坐标不相同,就要判断是否相交,相交则行不通
否则存图,用Dijkstra搜出最短的路径即可
还有,要注意细节
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <algorithm> #define eps 1e-6
#define INF 1000000000
typedef struct point
{
double x,y;
}point; typedef struct beline
{
point st,ed;
}beline; using namespace std; point p[];
double mp[][];
double d[];
int visit[]; bool dy(double x,double y){ return x > y+eps; }
bool xy(double x,double y){ return x < y-eps; }
bool dyd(double x,double y){ return x > y-eps; }
bool xyd(double x,double y){ return x < y+eps; }
bool dd(double x,double y){ return fabs(x - y)<eps; } double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
double Dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} bool onSegment(point a,point b,point c)
{
double maxx=max(a.x,b.x);
double maxy=max(a.y,b.y);
double minx=min(a.x,b.x);
double miny=min(a.y,b.y);
if(dd(crossProduct(a,b,c),0.0)&&dy(c.x,minx)&&xy(c.x,maxx)
&&dy(c.y,miny)&&xy(c.y,maxy))
return true;
return false;
} bool segIntersect(point p1,point p2,point p3,point p4)
{
double d1 = crossProduct(p3,p4,p1);
double d2 = crossProduct(p3,p4,p2);
double d3 = crossProduct(p1,p2,p3);
double d4 = crossProduct(p1,p2,p4);
if(xy(d1*d2,0.0)&&xy(d3*d4,0.0))
return true;
if(dd(d1,0.0)&&onSegment(p3,p4,p1))
return true;
if(dd(d2,0.0)&&onSegment(p3,p4,p2))
return true;
if(dd(d3,0.0)&&onSegment(p1,p2,p3))
return true;
if(dd(d4,0.0)&&onSegment(p1,p2,p4))
return true;
return false;
} void Dijkstra(int n)
{
int i,y;
memset(visit,,sizeof(visit));
for(i=; i<n; i++)
d[i] = mp[][i];
d[] = ;
for(i=; i<n; i++)
{
int m=INF,x;
{
for(y=; y<n; y++)
{
if(!visit[y] && d[y]<=m)
{
m = d[ x = y ];
}
}
visit[x]=;
for(y=; y<n; y++)
{
if(!visit[y] && d[y] > d[x]+mp[x][y])
{
d[y] = d[x] + mp[x][y];
}
}
}
}
} int main()
{
int n,m,i,j,k,t;
double a,b,c,d1,e;
beline li[];
beline tmp;
p[].x=;p[].y=;//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF && n!=-)
{
for(i=; i<; i++)
for(j=; j<; j++)
mp[i][j] = INF;
int cas=,css=;
for(i=; i<n; i++)
{
scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d1,&e);
li[css].st.x=a;
li[css].st.y=;
p[cas].x=a; li[css].ed.x=a;
p[cas++].y=b;li[css++].ed.y=b;
p[cas].x=a; li[css].st.x=a;
p[cas++].y=c;li[css].st.y=c;
p[cas].x=a; li[css].ed.x=a;
p[cas++].y=d1;li[css++].ed.y=d1;
p[cas].x=a; li[css].st.x=a;
p[cas++].y=e;li[css].st.y=e;
li[css].ed.x=a;
li[css++].ed.y=;
}
p[cas].x=10.0;p[cas].y=5.0;
for(i=; i<=cas; i++)
{
for(j=i+; j<=cas; j++)
{
int ok=;
for(k=; k<css; k++)
{
if(dd(p[i].x,p[j].x)||!dd(p[i].x,li[k].st.x)&&!dd(p[j].x,li[k].st.x)&&(segIntersect(p[i],p[j],li[k].st,li[k].ed)))
{
ok=;
break;
}
}
if(!ok)
{
mp[j][i] = mp[i][j] = Dist(p[i],p[j]);//printf("%d %d %lf ^^\n",i,j,mp[i][j]);
}
}
}
Dijkstra(cas+);
printf("%.2lf\n",d[cas]);
}
return ;
}
poj 1556 (Dijkstra + Geometry 线段相交)的更多相关文章
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- POJ 1556 计算几何 判断线段相交 最短路
题意: 在一个左下角坐标为(0,0),右上角坐标为(10,10)的矩形内,起点为(0,5),终点为(10,5),中间会有许多扇垂直于x轴的门,求从起点到终点在能走的情况下的最短距离. 分析: 既然是求 ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- POJ 1556 The Doors(线段交+最短路)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- POJ 1066 Treasure Hunt(线段相交判断)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4797 Accepted: 1998 Des ...
- POJ 2653 Pick-up sticks(线段相交)
题意:给定n个木棍依次放下,要求最终判断没被覆盖的木棍是哪些. 思路:快速排斥以及跨立实验可以判断线段相交. #include<algorithm> #include<cstdio& ...
- poj 3304(直线与线段相交)
传送门:Segments 题意:线段在一个直线上的摄影相交 求求是否存在一条直线,使所有线段到这条直线的投影至少有一个交点 分析:可以在共同投影处作原直线的垂线,则该垂线与所有线段都相交<==& ...
- [poj 1127]Jack Straws[线段相交][并查集]
题意: 给出一系列线段,判断某两个线段是否连通. 思路: 根据线段相交情况建立并查集, 在同一并查集中则连通. (第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..) 并 ...
- POJ 2653 Pick-up sticks [线段相交 迷之暴力]
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 12861 Accepted: 4847 D ...
随机推荐
- STM32内存跟FLASH问题
RO: 常量 ZI: 未初始化的全局变量 RW: 初始化的全局变量 Code: 程序本身 Code, RO-data,RW-data ..............flash RW-data, ZIda ...
- eclipse折叠快捷键
之前按代码折叠快捷键的时候发现时而灵时而不灵,今天突然发现了问题所在: 按ctrl+/(小键盘)使代码左边出现折叠标志 然后再按ctrl+shift+/(折叠) , ctrl+shift+*(展开 ...
- Terminal的快捷键 for Terminal for Mac OS 10.10, Linux/GNU(Ubuntu, deepin, elementory os,CentOS)
对于习惯用windows键盘的,突然转成Mac蓝牙键盘真的有点不习惯,尤其是多了⌘这个键,还有Alt键也成了Option 但是对于Windows下熟悉的快捷键,它们真的失效了,还好Ubuntu也常用, ...
- Java获取字符串编码方式
直接下载吧: http://files.cnblogs.com/files/xiluhua/BytesEncodingDetectTool.rar
- 在centos6.5-64bit上安装wxHexEditor,以查看编译二进制文件
目前在做一个存储,磁盘里面的数据老是出现很诡异的地方,某个通道的录像播放到一半的时候,切换到另外一个通道的视频上去了,一直不知道怎么下手,想着用十六进制编辑器查看磁盘数据. sudo yum inst ...
- 使用PHP flush 函数的时候我们需要注意些什么呢?
WebServer(可以认为特指apache)的缓冲区.在apache module的sapi下, flush会通过调用sapi_module的flush成员函数指针,间接的调用apache的api: ...
- Codeforces 735D:Taxes(哥德巴赫猜想)
http://codeforces.com/problemset/problem/735/D 题意:给出一个n,这个n可以分解成 n = n1 + n2 + -- + nk,其中k可以取任意数.要使得 ...
- 如何修改ECSHOP后台管理中心的Title信息
下图中红色圈定的部分就是本次修改要改的地方 修改方法其实很简单的:打开语言包文件 /languages/zh_cn/admin/common.php 将 $_LANG['app_name'] = ' ...
- PRINCE2七大原则(2)
PRINCE2七大原则(2) 我们先来回顾一下,PRINCE2七大原则分别是持续的业务验证,经验学习,角色与责任,按阶段管理,例外管理,关注产品,剪裁. 第二个原则:吸取经验教训. PRINCE2要求 ...
- JavaScript DOM 编程艺术(第2版)读书笔记(1)
JavaScript 简史 JavaScript 是Netscape公司与Sun公司合作开发的.在 JavaScript 1.0发布时,Netscape Navigator主宰着浏览器市场.微软在推出 ...