[线段判交][最短路]The Doors POJ1556

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length.

[线段判交][最短路]The Doors POJ1556

Input

The input data for the illustrated chamber would appear as follows.

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

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

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

题意: 有一个10*10的正方形区域,其中有n堵竖直的墙,每堵墙上有两段开口可以*通过,如上图所示。询问从起点(0,5)到终点(10,5)的最短距离。

分析: 显然是个最短路问题,不过在套最短路模板前先要把图建出来。建图的过程用到了计算几何的一些知识。首先可以把所有的墙作为线段保存下来,之后枚举图中每一对点,如果这两点构成线段与任何一堵墙都不规范相交,则这两点间可以连边。这里的点集就是每堵墙上开口的两端点,至于为什么一定要走端点而不从中间穿过,假设走完这步之后下一步要走到(x,y),从0枚举y的位置到10发现走中间一定不是这两步中最优的。

建好图后一个dijkstra就结束了,最后一定注意n=0的情况,此时特殊处理,否则起点终点无法连边。

具体代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <utility>
#include <queue>
#include <cmath>
#define pii pair<int, int>
const double eps = 1e-8;
const double inf = 1e20;
const double pi = acos(-1.0);
using namespace std;

int sgn(double x)
{
	if(fabs(x) < eps)return 0;
	if(x < 0)return -1;
	else return 1;
}
struct Point
{
	double x, y;
	Point(){}
	Point(double _x,double _y){x = _x, y = _y;}
	void input(){scanf("%lf%lf",&x,&y);}
	void output(){printf("%.2f %.2f\n",x,y);}
	bool operator == (Point b)const{return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;}
	bool operator < (Point b)const{return sgn(x-b.x)== 0?sgn(y-b.y)<0:x<b.x;}
	Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}
	//叉积
	double operator ^(const Point &b)const{return x*b.y - y*b.x;}
	//点积
	double operator *(const Point &b)const{return x*b.x + y*b.y;}
	//返回长度
	double len(){return hypot(x,y);/*库函数*/}
	//返回长度的平方
	double len2(){return x*x + y*y;}
	//返回两点的距离
	double distance(Point p){return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));}
	Point operator +(const Point &b)const{return Point(x+b.x,y+b.y);}
	Point operator *(const double &k)const{return Point(x*k,y*k);}
	Point operator /(const double &k)const{return Point(x/k,y/k);}
	//`计算pa  和  pb 的夹角`
	//`就是求这个点看a,b 所成的夹角`
	//`测试 LightOJ1203`
	double rad(Point a,Point b)
	{
		Point p = *this;
		return fabs(atan2( fabs((a-p)^(b-p)),(a-p)*(b-p) ));
	}
	//`化为长度为r的向量`
	Point trunc(double r)
	{
		double l = len();
		if(!sgn(l))return *this;
		r /= l;
		return Point(x*r,y*r);
	}
	//`逆时针旋转90度`
	Point rotleft(){return Point(-y,x);}
	//`顺时针旋转90度`
	Point rotright(){return Point(y,-x);}
	//`绕着p点逆时针旋转angle`
	Point rotate(Point p,double angle)
	{
		Point v = (*this) - p;
		double c = cos(angle), s = sin(angle);
		return Point(p.x + v.x*c - v.y*s,p.y + v.x*s + v.y*c);
	}
}p[105]; 
struct Line
{
	Point s,e;
	Line(){}
	Line(Point _s,Point _e){s = _s, e = _e;}
	bool operator ==(Line v){return (s == v.s)&&(e == v.e);}
	//`根据一个点和倾斜角angle确定直线,0<=angle<pi`
	Line(Point p,double angle)
	{
		s = p;
		if(sgn(angle-pi/2) == 0){e = (s + Point(0,1));}
		else{e = (s + Point(1,tan(angle)));}
	}
	//ax+by+c=0
	Line(double a,double b,double c)
	{
		if(sgn(a) == 0)	s = Point(0,-c/b), e = Point(1,-c/b);
		else if(sgn(b) == 0) s = Point(-c/a,0), e = Point(-c/a,1);
		else s = Point(0,-c/b), e = Point(1,(-c-a)/b);
	}
	void input()
	{
		s.input();
		e.input();
	}
	void adjust(){if(e < s)swap(s,e);}
	//求线段长度
	double length(){return s.distance(e);}
	//`返回直线倾斜角 0<=angle<pi`
	double angle()
	{
		double k = atan2(e.y-s.y,e.x-s.x);
		if(sgn(k) < 0)k += pi;
		if(sgn(k-pi) == 0)k -= pi;
		return k;
	}
	//`点和直线关系`
	//`1  在左侧`
	//`2  在右侧`
	//`3  在直线上`
	int relation(Point p)
	{
		int c = sgn((p-s)^(e-s));
		if(c < 0)return 1;
		else if(c > 0)return 2;
		else return 3;
	}
	// 点在线段上的判断
	bool pointonseg(Point p){return sgn((p-s)^(e-s)) == 0 && sgn((p-s)*(p-e)) <= 0;}
	//`两向量平行(对应直线平行或重合)`
	bool parallel(Line v){return sgn((e-s)^(v.e-v.s)) == 0;/*两向量叉积为0*/ }
	//`两线段相交判断`
	//`2 规范相交`
	//`1 非规范相交`
	//`0 不相交`
	int segcrossseg(Line v)
	{
		int d1 = sgn((e-s)^(v.s-s));
		int d2 = sgn((e-s)^(v.e-s));
		int d3 = sgn((v.e-v.s)^(s-v.s));
		int d4 = sgn((v.e-v.s)^(e-v.s));
		if( (d1^d2)==-2 && (d3^d4)==-2 )return 2;
		return (d1==0 && sgn((v.s-s)*(v.s-e))<=0) ||
			(d2==0 && sgn((v.e-s)*(v.e-e))<=0) ||
			(d3==0 && sgn((s-v.s)*(s-v.e))<=0) ||
			(d4==0 && sgn((e-v.s)*(e-v.e))<=0);
	}
	//`直线和线段相交判断`
	//`-*this line   -v seg`
	//`2 规范相交`
	//`1 非规范相交`
	//`0 不相交`
	int linecrossseg(Line v)
	{
		int d1 = sgn((e-s)^(v.s-s));
		int d2 = sgn((e-s)^(v.e-s));
		if((d1^d2)==-2) return 2;
		return (d1==0||d2==0);
	}
	//`两直线关系`
	//`0 平行`
	//`1 重合`
	//`2 相交`
	int linecrossline(Line v)
	{
		if((*this).parallel(v))//此时平行或者重合 
			return v.relation(s)==3;//如果当前直线起点在另一条直线上 
		return 2;
	}
	//`求两直线的交点`
	//`要保证两直线不平行或重合`
	Point crosspoint(Line v)
	{
		double a1 = (v.e-v.s)^(s-v.s);
		double a2 = (v.e-v.s)^(e-v.s);
		return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));
	}
	//点到直线的距离
	double dispointtoline(Point p){return fabs((p-s)^(e-s))/length();}
	//点到线段的距离
	double dispointtoseg(Point p)
	{
		if(sgn((p-s)*(e-s))<0 || sgn((p-e)*(s-e))<0)
			return min(p.distance(s),p.distance(e));
		return dispointtoline(p);
	}
	//`返回线段到线段的距离`
	//`前提是两线段不相交,相交距离就是0了`
	double dissegtoseg(Line v){return min(min(dispointtoseg(v.s),dispointtoseg(v.e)),min(v.dispointtoseg(s),v.dispointtoseg(e)));}
	//`返回点p在直线上的投影`
	Point lineprog(Point p){return s + ( ((e-s)*((e-s)*(p-s)))/((e-s).len2()) );}
	//`返回点p关于直线的对称点`
	Point symmetrypoint(Point p)
	{
		Point q = lineprog(p);
		return Point(2*q.x-p.x,2*q.y-p.y);
	}
}l[105];

double dis[105];
int head[105], vis[105], cnt, n;
struct edge
{
	int to, next;
	double w;
}edge[20005];

void add(int u, int v, double w)
{
	edge[++cnt].to = v;
	edge[cnt].w = w;
	edge[cnt].next = head[u];
	head[u] = cnt;
}

void dijkstra()
{
	dis[4*n+1] = 0;
	priority_queue<pii, vector<pii>, greater<pii> > a;
	a.push(make_pair(0, 4*n+1));
	while(a.size())
	{
		int now = a.top().second;
		a.pop();
		if(vis[now])
			continue;
		vis[now] = true;
		for(int i = head[now]; i; i = edge[i].next)
		{
			int to = edge[i].to;
			if(dis[to] > dis[now]+edge[i].w)
			{
				dis[to] = dis[now]+edge[i].w;
				a.push(make_pair(dis[to], to));
			}
		}
	}
}

signed main()
{
	while(~scanf("%d", &n))
	{
		if(n == -1)
			break;
		int t = 0; 
		for(int i = 1; i <= n; i++)
		{
			double x, y1, y2, y3, y4;
			scanf("%lf%lf%lf%lf%lf", &x, &y1, &y2, &y3, &y4);
			Point p1(x, y1), p2(x, y2), p3(x, y3), p4(x, y4);
			l[++t].s = Point(x, 0);
			l[t].e = p1;
			l[++t].s = p2;
			l[t].e = p3;
			l[++t].s = p4;
			l[t].e = Point(x, 10);
			p[4*i-3] = p1;
			p[4*i-2] = p2;
			p[4*i-1] = p3;
			p[4*i] = p4;
		}
		p[4*n+1].x = 0, p[4*n+1].y = 5;//起点 
		p[4*n+2].x = 10, p[4*n+2].y = 5;//终点 
		cnt = 0;
		for(int i = 1; i <= 4*n+2; i++)
			head[i] = vis[i] = 0, dis[i] = inf;
		//建图
		if(n == 0)
			dis[1] = 0, dis[2] = 10;
		else
		{
			for(int i = 1; i <= 4*n+2; i++)
				for(int j = i+1; j <= 4*n+2; j++)
				{
					Line temp(p[i], p[j]);
					if(!sgn(p[i].x-p[j].x))
						continue;
					for(int k = 1; k <= t; k++)//k堵墙 
					{
						if(temp.segcrossseg(l[k]) == 2)
							break;
						if(k == t)
						{
							add(i, j, temp.length());
							add(j, i, temp.length());
						}
					}
				} 
		}
		dijkstra();
		printf("%.2f\n", dis[4*n+2]);
	}
	return 0;
}

上一篇:Atitit 控制中心快速启动面板quick launcher


下一篇:Key Task bfs()找到最优值