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 ...
随机推荐
- Base Enum Properties [AX 2012]
Base Enum Properties [AX 2012] This topic has not yet been rated - Rate this topic Updated: December ...
- 分享:shell去掉linux配置文件的注释行
如何通过shell去掉Linux配置文件中的注释行呢? 本文给出两种解决方法,供大家参考. 方法1.使用grep -v "^#" 来去掉注释行,其中:-v 表示取反 ^# 表示注解 ...
- Nagios监控磁盘
1.查看check_disk脚本 [oracle@rhel5 ~]$ /usr/local/nagios/libexec/check_disk --h check_disk v1.) Copyrigh ...
- node-webkit 新建实例窗口间通信问题解决办法
终于弄明白这问题了,只要在js文件里加上段代码,就可解决两窗口间通信问题. var str = { username: User.name, userrole: User.role }; var ne ...
- php header()函数设置页面Cache缓存
header()函数在php的使用很大,下面我来介绍利用它实现页面缓存的一些方法,但使用header前必须注意,在它之前不能任何输出,包括空格. 手册上,我们对于cache都是写着如何设置,以便让代码 ...
- jquery中的节点的操作
节点的操作 Dom 文档对象 模型 解决 一.插入节点 Append() 在每个匹配的元素中追加内容 Var $li_1= “<li></li>”; Var $li_2 = ...
- [ios][swift]swift 怎么去除 optional
在转换String时要使用“!”进行拆包,用“?”则会有optional 出现
- YTU 2297: KMP模式匹配 三(串)
2297: KMP模式匹配 三(串) 时间限制: 1 Sec 内存限制: 128 MB 提交: 25 解决: 16 [提交][状态][讨论版] [Edit] [TestData] 题目描述 输入一 ...
- 20151124001 关闭C#主窗体弹出是否关闭对话框
关闭C#主窗体弹出是否关闭对话框 private void Frm_Main_FormClosing(object sender, FormClosingEventArgs e) { ...
- poj3667 Hotel
此题不难却易出错,很能考察思维的严谨性. 指定ll为区间内左端顶格数的连续可利用房间,rr为右端顶格数的数值,mm为区间内最长的连续可利用房间数. 在查询的时候,由于要返回最靠左的区间左端点,使得在该 ...