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 ...
随机推荐
- sql创建删除修改表的基本操作
1 建立表格 在建立好数据库以后,就可以根据储存资料的需求,使用SQL叙述建立所有需要的表格(table).建立表格的设定非常多,以建立"world.city"表格来说,它的叙述会 ...
- 关于 MySQL LEFT JOIN 你可能需要了解的三点
即使你认为自己已对 MySQL 的 LEFT JOIN 理解深刻,但我敢打赌,这篇文章肯定能让你学会点东西! ON 子句与 WHERE 子句的不同 一种更好地理解带有 WHERE ... IS NUL ...
- iOS 学习笔记 七 (2015.03.29)code snippet操作
1.code snippet 备份路径:~/Library/Developer/Xcode/UserData/CodeSnippets/
- sersync实现触发式同步
金山的一个居于inotify+rsync进行二次开发实现文件同步的小工具sersync,能够很方便的实现文件触发式同步 Inotify 是基于inode级别的文件系统监控技术,是一种强大的.细粒度的. ...
- jQuery中attr() 和 prop()【转】
Version 1.9.0 开始不建议使用 attr() 来对具有 true 和 false 两个属性的属性进行操作. 具有 true 和 false 两个属性的属性,如 checked, selec ...
- Java获取字符串编码方式
直接下载吧: http://files.cnblogs.com/files/xiluhua/BytesEncodingDetectTool.rar
- WPF:窗体置顶
1.设置窗体TopMost属性 private DispatcherTimer timer; public Window1() { InitializeComponent(); Loaded += n ...
- Android Studio解决unspecified on project app resolves to an APK archive which is not supported
出现该问题unspecified on project app resolves to an APK archive which is not supported as a compilation d ...
- JAVA字段的初始化规律
JAVA字段的初始化规律 1.类的构造方法 (1)“构造方法”,也称为“构造函数”,当创建一个对象时,它的构造方法会被自动调用.构造方法与类名相同,没有返回值. (2)如果类没有定义构造函数,Java ...
- Android动画的使用总结
1.补间动画(透明渐变.平移.旋转.缩放.组合) 方法一:通过xml文件设置 1-1:创建:res/anim 1-2:java代码写调用 Animation a = AnimationUtils.lo ...