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 ...
随机推荐
- TI BLE CC2541的I2C主模式
由于要写TM1680, 写命令跟写数据, 所以需要使用CC2541的I2C, 2541是有硬件I2C的. tm1680.c: #include "tm1680.h" //TM168 ...
- C# 控制连接超时
首先连接超时分为三种,TCP Connection to SQL Server -> SqlConnection.Open -> SqlCommand.Execute先说第二种超时,sql ...
- ExtJS4.2 根据数据库记录构建树形菜单
背景:最近用ExtJS4.2做一个系统,需要在前端展示资源菜单,为树形结构,该树形结构是从数据库动态加载的. ExtJS的树形结构大致有两种情况: 1.静态树形结构,此处不多说,看API就能简单明白: ...
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.9.SportsStore:Securing the Administration Features
1 设置表单身份认证 因为ASP.NET MVC基于ASP.NET平台的核心,所以我们可以使用ASP.NET Form的身份认证,这是保持用户登录轨迹通用的方法.现在介绍最基本的配置. 在Web.co ...
- PHP编写的图片验证码类文件分享方法
适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...
- thinkphp模板中截取中文字符串的方法分享
前段用thinkphp写了一个系统,感觉thinkphp学起来比较容易,开发起来了比较顺手,其中一个关键的因素就是它的模版引擎相当强大,使用方法跟smarty类似,在模版中还可以用php代码,有模版包 ...
- PHP serialize & JSON 解析
对于JSON(JavaScript Object Notation)大家应该不陌生,它是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Program ...
- mount/umount系统调用
转载自:http://blog.sina.com.cn/s/blog_6385c7310100iqmn.html 功能描述: mount挂上文件系统,umount执行相反的操作. 用法: # ...
- Python相对路径转绝对路径,绝对路径转相对路径
1.绝对路径转相对路径 print os.path.relpath("d:/MyProj/MyFile.txt") #..\MyProj\MyFile.txt 是根据当前路径的相对 ...
- Socket状态变迁图
在一些防火墙或端口管理工具中经常会看到连接状态为CLOSED CLOSE_WITE LAST_ACK等的进程, 虽然状态就那么很少的几个, 而且看字面意思也能猜个大概, 但没做过Socket编程的朋友 ...