题意:

一个正方形中有n道竖直的墙,每道墙上开两个门。求从左边中点走到右边中点的最短距离。

分析:

以起点终点和每个门的两个端点建图,如果两个点可以直接相连(即不会被墙挡住),则权值为两点间的欧几里得距离。

然后求起点到终点的最短路即可。

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; const int maxn = ;
const double INF = 1e4;
const double eps = 1e-; struct Point
{
double x, y;
Point(double x=, double y=):x(x), y(y) {}
}p[maxn * ]; typedef Point Vector; Point read_point()
{
double x, y;
scanf("%lf%lf", &x, &y);
return Point(x, y);
} Point operator - (const Point& A, const Point& B)
{ return Point(A.x-B.x, A.y-B.y); } Vector operator / (const Vector& A, double p)
{ return Vector(A.x/p, A.y/p); } double Dot(const Vector& A, const Vector& B)
{ return A.x*B.x + A.y*B.y; } double Length(const Vector& A)
{ return sqrt(Dot(A, A)); } struct Door
{
double x, y1, y2, y3, y4;
Door(double x=, double y1=, double y2=, double y3=, double y4=):x(x), y1(y1), y2(y2), y3(y3), y4(y4) {}
}; vector<Door> door; double d[maxn * ], w[maxn * ][maxn * ];
bool vis[maxn * ];
int cnt; bool isOK(int a, int b)
{//判断两点是否能直接到达
if(p[a].x >= p[b].x) swap(a, b);
for(int i = ; i < door.size(); ++i)
{
if(door[i].x <= p[a].x) continue;
if(door[i].x >= p[b].x) break;
double k = (p[b].y-p[a].y) / (p[b].x-p[a].x);
double y = p[a].y + k * (door[i].x - p[a].x);
if(!(y>=door[i].y1&&y<=door[i].y2 || y>=door[i].y3&&y<=door[i].y4)) return false;
}
return true;
} void Init()
{
for(int i = ; i < cnt; ++i)
for(int j = i; j < cnt; ++j)
if(i == j) w[i][j] = ;
else w[i][j] = w[j][i] = INF;
} int main()
{
//freopen("in.txt", "r", stdin); int n;
while(scanf("%d", &n) == && n + )
{
door.clear();
memset(vis, false, sizeof(vis));
memset(d, , sizeof(d)); p[] = Point(, );
cnt = ;
for(int i = ; i < n; ++i)
{
double x, y[];
scanf("%lf", &x);
for(int j = ; j < ; ++j) { scanf("%lf", &y[j]); p[cnt++] = Point(x, y[j]); }
door.push_back(Door(x, y[], y[], y[], y[]));
}
p[cnt++] = Point(, ); Init(); for(int i = ; i < cnt; ++i)
for(int j = i+; j < cnt; ++j)
{
double l = Length(Vector(p[i]-p[j]));
if(p[i].x == p[j].x) continue;
if(isOK(i, j))
w[i][j] = w[j][i] = l;
}
//Dijkstra
d[] = ;
for(int i = ; i < cnt; ++i) d[i] = INF;
for(int i = ; i < cnt; ++i)
{
int x;
double m = INF;
for(int y = ; y < cnt; ++y) if(!vis[y] && d[y] <= m) m = d[x=y];
vis[x] = ;
for(int y = ; y < cnt; ++y) d[y] = min(d[y], d[x] + w[x][y]);
} printf("%.2f\n", d[cnt-]);
} return ;
}

代码君

POJ (线段相交 最短路) The Doors的更多相关文章

  1. POJ_1556_The Doors_判断线段相交+最短路

    POJ_1556_The Doors_判断线段相交+最短路 Description You are to find the length of the shortest path through a ...

  2. 简单几何(线段相交+最短路) POJ 1556 The Doors

    题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...

  3. POJ 1556 The Doors(线段相交+最短路)

    题目: Description You are to find the length of the shortest path through a chamber containing obstruc ...

  4. POJ 2556 (判断线段相交 + 最短路)

    题目: 传送门 题意:在一个左小角坐标为(0, 0),右上角坐标为(10, 10)的房间里,有 n 堵墙,每堵墙都有两个门.每堵墙的输入方式为 x, y1, y2, y3, y4,x 是墙的横坐标,第 ...

  5. POJ 1556 计算几何 判断线段相交 最短路

    题意: 在一个左下角坐标为(0,0),右上角坐标为(10,10)的矩形内,起点为(0,5),终点为(10,5),中间会有许多扇垂直于x轴的门,求从起点到终点在能走的情况下的最短距离. 分析: 既然是求 ...

  6. POJ 1556 The Doors【最短路+线段相交】

    思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...

  7. POJ 1556 The Doors(线段交+最短路)

    The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5210   Accepted: 2124 Descrip ...

  8. POJ 1556 The Doors(线段交+最短路)

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  9. poj 1066 线段相交

    链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

随机推荐

  1. PHP随机生成广告图片的实例 代码

    PHP随机生成广告图片: <?php /*  +------------------------------------------------------------------+  | Mi ...

  2. Pox组件

    最近在学习Pox,为了加深印象,对Pox wiki中的Pox组件写了些笔记.   按照组件的功能进行分类:   L2层地址学习.洪泛 forwarding.hub forwarding.l2_lear ...

  3. Nodejs & Mongod

    http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/

  4. esp8266的STM32驱动

    esp8266的STM32驱动,数据发送接收由DMA完成,释放CPU. 目前只能发送返回消息为成功或失败的AT命令,并判断是否成功,详见esp8266_cmd():其它返回消息不可预知的命令(如查看A ...

  5. 实战开发中UI资源制作标准

    资源制作标准设定建议 1.所有的UI资源全部采用PNG导出 因为Unity不支持外部压缩,所以,不论是用PNG还是JPG,只要尺寸相同,资源量在引擎中都会是一样大.所以,可以大胆地采用PNG进行输出, ...

  6. php实现调用微信上传照片然后保存至服务器与数据库

    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>    <s ...

  7. js 人工获取年月日

    var date = new Date(); var months = new Array("01", "02", "03", " ...

  8. Elasticsearch从0.90(0.90.x)到1.2(1.x)API的变化-二

    本文为官方文档的译文加个人理解.作者翻译时,elasticsearch(下面简称es)的版本为1.2.2. 请支持原创:http://www.cnblogs.com/donlianli/p/38367 ...

  9. linux学习笔记(3)-文件系统

    三大类文件类型 普通文件:包括文本文件.数据文件.可执行的二进制程序文件 目录文件:linux系统把目录看成一种特殊的文件,利用它构成了文件系统的树形结构 设备文件:把设备也看成是一个文件,例如你的鼠 ...

  10. What are the advantages of logistic regression over decision trees?FAQ

    What are the advantages of logistic regression over decision trees?FAQ The answer to "Should I ...