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 ...
随机推荐
- javascript中的this与prototype,原型理解
JavaScript 函数调用 JavaScript 函数有 4 种调用方式. 每种方式的不同方式在于 this 的初始化. this 关键字 一般而言,在Javascript中,this指向函数执行 ...
- USB HID介绍【转】
本文转载自:http://blog.csdn.net/leo_wonty/article/details/6721214 HID是一种USB通信协议,无需安装驱动就能进行交互,在学习HID之前,先来复 ...
- halcon学习笔记——机器视觉工程应用的开发思路【转】
转自:http://www.cnblogs.com/hanzhaoxin/archive/2013/02/15/2912879.html 机器视觉工程应用主要可划分为硬件和软件两大部分. 硬件:工程应 ...
- Linux Kernel之flush_cache_all在ARM平台下是如何实现的【转】
转自:http://blog.csdn.net/u011461299/article/details/10199989 版权声明:本文为博主原创文章,未经博主允许不得转载. 在驱动程序的设计中,我们可 ...
- DedeCMS模板文件不存在,无法解析文档! 问题定位方法
生成静态的时候,经常会遇到“模板文件不存在,无法解析文 档!”的问题.很多朋友试过论坛里很多方法,都是针对某些人可以解决,某些人的问题依旧,为什么呢?其实问题很可能确实是多种多样的,表现结果却是一样, ...
- sql 语法
CASE ISNULL(b.enddate , '2000-1-1') WHEN '2000-1-1' THEN '未发稿' ELSE '已经发稿' END 如果时间为空,则显示为值‘200-1-1’ ...
- two day python基础知识
1.调用功能 ---- -在同一个目录下,调用用户名密码登陆模块 2.创建文件夹 import os #os模块 os.mkdir ("new_dd3")# 创建文件夹 三元 3. ...
- 求两个数的最大公约数(Java)
获得两个随机数(100以内),并放入数组中 public int[] getTwoRandom(){ int[] t = new int[2]; Random rand = new Random(); ...
- Help Me with the Game 分类: POJ 2015-06-29 16:34 17人阅读 评论(0) 收藏
Help Me with the Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3706 Accepted: ...
- 从hadoop框架与MapReduce模式中谈海量数据处理
http://blog.csdn.net/wind19/article/details/7716326 前言 几周前,当我最初听到,以致后来初次接触Hadoop与MapReduce这两个东西,我便稍显 ...