The Doors
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6231 | Accepted: 2499 |
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
Source
本题难点在于建图,枚举所有方向向右的连线并去掉被阻隔的,然后任意一种最短路。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double INF=10000000.0;
int u[10000],v[10000];
double g[100][100],py[20][6],wx[20],dis[100],w[10000];
struct node
{
double x,y;
node(double xx=0,double yy=0)
{
x=xx,y=yy;
}
}p[100];
int n,m,vcnt,ecnt;
double dist(node a,node b)
{
return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cross(double x1,double y1,double x2,double y2,double x3,double y3)
{
return (x2-x1)*(y3-y1)-(x3-x1)*(y2-y1);
}
bool is_ok(node a,node b)
{
if(a.x>=b.x)return 0;
int i=0;
while(wx[i]<=a.x&&i<n)
i++;
while(wx[i]<b.x&&i<n)
{
if(cross(a.x,a.y,b.x,b.y,wx[i],0)*cross(a.x,a.y,b.x,b.y,wx[i],py[i][0])<0
||cross(a.x,a.y,b.x,b.y,wx[i],py[i][1])*cross(a.x,a.y,b.x,b.y,wx[i],py[i][2])<0
||cross(a.x,a.y,b.x,b.y,wx[i],py[i][3])*cross(a.x,a.y,b.x,b.y,wx[i],10)<0)
return 0;
i++;
}
return 1;
}
double bellman()
{
int i,j,k;
for(i=1;i<vcnt;i++)dis[i]=INF;
dis[0]=0;
for(i=1;i<=vcnt;i++)
for(j=0;j<ecnt;j++)
if(dis[v[j]]>dis[u[j]]+w[j])dis[v[j]]=dis[u[j]]+w[j];
return dis[vcnt-1];
}
int main()
{
while(~scanf("%d",&n)&&n!=-1)
{
int i,j;double k;
vcnt=0;
p[vcnt++]=node(0,5);
for(i=0;i<n;i++)
{
scanf("%lf",&wx[i]);//wx存储墙的横坐标
for(j=0;j<4;j++)
{
scanf("%lf",&k);
p[vcnt++]=node(wx[i],k);
py[i][j]=k;//第i面墙(从0数)第j个点
}
}
p[vcnt++]=node(10,5);//存点完成,开始存边
ecnt=0;
for(i=0;i<vcnt;i++)
for(j=0;j<vcnt;j++)
g[i][j]=INF;
for(i=0;i<vcnt;i++)
for(j=i+1;j<vcnt;j++)
if(is_ok(p[i],p[j]))//去掉被截断的
{
u[ecnt]=i;
v[ecnt]=j;
g[i][j]=dist(p[i],p[j]);
w[ecnt++]=g[i][j];
}
printf("%.2f\n",(float)bellman());//double的我wa了,网上据说是double的精度问题
}
return 0;
}