题目链接:传送门
The Doors
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8775 | Accepted: 3367 |
Description
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.
![](https://i-blog.csdnimg.cn/blog_migrate/645e625e62d18e2c5da4d99ab0713357.jpeg)
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.
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
解题思路:从始点到终点沿着墙的端点走才能得到最短路,所以从始点到墙的端点建边,墙的端点之间建边,墙的端点到终点建边,始点到终点建边(两点连成的线段不穿过任意墙边才存在),边权为两点间的距离。然后跑遍最短路求得始点到终点的最短路。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 80;
const int M = 2000;
const double INF = 0x3fffffff;
const double eps = 1e-8;
const double PI = acos(-1.0);
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;
}
Point operator - (const Point&a)const{
return Point(a.x-x,a.y-y);
}
double operator ^ (const Point&a)const{
return a.x*y-a.y*x;
}
double operator * (const Point&a)const{
return a.x*x+a.y*y;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e){
s = _s; e = _e;
}
};
//两点间的距离
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
//判断线段是否相交
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) > min(l2.s.x,l2.e.x) &&
max(l1.s.y,l1.e.y) > min(l2.s.y,l2.e.y) &&
max(l2.s.x,l2.e.x) > min(l1.s.x,l1.e.x) &&
max(l2.s.y,l2.e.y) > min(l1.s.y,l1.e.y) &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) < 0 &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) < 0;
}
Line line[20][4]; //每堵墙的边
Point point[20][4]; //每堵墙的点
int Index[20][4]; //每堵墙上的点对应图中的序号
int S,E; //起点和终点在图中的序号
Point Ps,Pe; //起点和终点
void Read( int n )
{
double x,y1,y2,y3,y4; int cnt = 0;
for( int i = 0 ; i < n ; ++i ){
scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
line[i][0] = Line(Point(x,0.0),Point(x,y1));
line[i][1] = Line(Point(x,y2),Point(x,y3));
line[i][2] = Line(Point(x,y4),Point(x,10.0));
point[i][0] = Point(x,y1);
point[i][1] = Point(x,y2);
point[i][2] = Point(x,y3);
point[i][3] = Point(x,y4);
Index[i][0] = ++cnt;
Index[i][1] = ++cnt;
Index[i][2] = ++cnt;
Index[i][3] = ++cnt;
}
S = ++cnt; E = ++cnt;
}
struct Edge{
int node;
double len;
Edge*next;
}m_edge[M*2];
Edge*head[N];
int Ecnt,vis[N];
double dis[N];
void init()
{
Ecnt = 0;
fill( head , head+N , (Edge*)0 );
}
void mkEdge( int a , int b , double c )
{
m_edge[Ecnt].node = b;
m_edge[Ecnt].len = c;
m_edge[Ecnt].next = head[a];
head[a] = m_edge+Ecnt++;
}
void spfa( int u )
{
fill( vis , vis+N , 0 );
fill( dis , dis+N , INF );
dis[u] = 0.0;
queue<int>nd;
nd.push(u);
vis[u] = 1;
while( !nd.empty() ){
int s = nd.front();
nd.pop();
vis[s] = 0;
for( Edge*p = head[s] ; p ; p = p->next ){
int t = p->node;
if( dis[t] > dis[s]+p->len ){
dis[t] = dis[s]+p->len;
if( !vis[t] ){
nd.push(t);
vis[t] = 1;
}
}
}
}
}
//判断某两点连成的边是否会与墙相交
bool judge( Line L , int n )
{
for( int i = 0 ; i < n ; ++i ){
//每堵墙有三条边
for( int j = 0 ; j < 3 ; ++j ){
if( inter(L,line[i][j]) ){
//cout << "aaa" << endl;
//cout << Index[i][j] << endl;
return false;
}
}
}
return true;
}
//起点S到其他点建边
void StoPoint( int n )
{
for( int i = 0 ; i < n ; ++i ){
//每堵墙四个点
for( int j = 0 ; j < 4 ; ++j ){
if( judge(Line(Ps,point[i][j]),n) )
mkEdge(S,Index[i][j],dist(Ps,point[i][j]));
}
}
}
//终点E到其他点建边
void EtoPoint( int n )
{
for( int i = 0 ; i < n ; ++i ){
for( int j = 0 ; j < 4 ; ++j ){
if( judge(Line(Pe,point[i][j]),n) )
mkEdge(Index[i][j],E,dist(Pe,point[i][j]));
}
}
}
//墙上的点到墙上的其他点建边
void PtoPoint( int n )
{
for( int i = 0 ; i < n ; ++i )for( int j = 0 ; j < 4 ; ++j ){
for( int k = i+1 ; k < n ; ++k )for( int m = 0 ; m < 4 ; ++m ){
if( judge(Line(point[i][j],point[k][m]),n) )
mkEdge(Index[i][j],Index[k][m],dist(point[i][j],point[k][m]));
}
}
}
//起点到终点建边
void StoE( int n )
{
if( judge(Line(Ps,Pe),n) ) mkEdge(S,E,10.0);
}
int main()
{
int n;
while( ~scanf("%d",&n)&&n != -1 ){
init();
Ps = Point(0.0,5.0);
Pe = Point(10.0,5.0);
Read(n);
StoPoint(n);
StoE(n);
EtoPoint(n);
PtoPoint(n);
spfa(S);
printf("%.2lf\n",dis[E]);
}
return 0;
}