The Doors
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8334 | Accepted: 3218 |
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.
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
Source
题意:从(0,5)走到(10,5)最短路
我太傻逼了,查了好长时间计算几何的错,结果是求DAG的DP忘清空vis了
线段相交做两个直线与线段相交就行了
注意本题一个端点在另一条线上不能算相交哦
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=,M=1e4+;
const double INF=1e9;
const double eps=1e-;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
inline int sgn(double x){
if(abs(x)<eps) return ;
else return x<?-:;
}
struct Vector{
double x,y;
Vector(double a=,double b=):x(a),y(b){}
bool operator <(const Vector &a)const{
return x<a.x||(x==a.x&&y<a.y);
}
void print(){
printf("%lf %lf\n",x,y);
}
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==&&sgn(a.y-b.y)==;} double Cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
double DisPP(Point a,Point b){
Point t=a-b;
return sqrt(t.x*t.x+t.y*t.y);
}
struct Line{
Point s,t;
Line(){}
Line(Point p,Point v):s(p),t(v){}
}l[N];
int cl;
bool isLSI(Line l1,Line l2){
Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
return sgn(Cross(v,u))!=sgn(Cross(v,w))&&sgn(Cross(v,u))!=&&sgn(Cross(v,w))!=;
}
bool isSSI(Line l1,Line l2){
return isLSI(l1,l2)&&isLSI(l2,l1);
}
bool can(Point a,Point b){
Line line(a,b);
for(int i=;i<=cl;i++)
if(isSSI(l[i],line)) return false;
return true;
} int n,s,t;
struct edge{
int v,ne;
double w;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v,double w){//printf("ins %d %d %lf\n",u,v,w);
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
}
double d[N];
int vis[N]; double dp(int u){
if(vis[u]) return d[u];
vis[u]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
d[u]=min(d[u],dp(v)+e[i].w);
}
return d[u];
}
void DAG(){
for(int i=s;i<=t;i++) d[i]=INF;
memset(vis,,sizeof(vis));
d[t]=;vis[t]=;
dp(s);
} Point p[N][];
Point S(,),T(,);
inline int idx(int u){return u%==?u/:u/+;}
inline int idy(int u){return u%==?:u%;}
double x;
int main(int argc, const char * argv[]) {
while(true){
n=read();s=;t=*n+;
if(n==-) break;
cnt=;memset(h,,sizeof(h));
cl=; for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf%lf",&x,&p[i][].y,&p[i][].y,&p[i][].y,&p[i][].y);
p[i][].x=p[i][].x=p[i][].x=p[i][].x=x;
int num=(i-)*;
//for(int j=1;j<=4;j++) p[i][j].print();
if(i==){
for(int j=;j<=;j++)
ins(s,num+j,DisPP(S,p[i][j]));
}else{
for(int j=;j<=;j++){
for(int u=;u<=num;u++){
if(can(p[idx(u)][idy(u)],p[i][j]))
ins(u,num+j,DisPP(p[idx(u)][idy(u)],p[i][j]));
}
if(can(S,p[i][j])) ins(s,num+j,DisPP(S,p[i][j]));
}
}
l[++cl]=Line(Point(x,),p[i][]);
l[++cl]=Line(p[i][],p[i][]);
l[++cl]=Line(p[i][],Point(x,));
}
int num=n*;
for(int u=;u<=num;u++)
if(can(p[idx(u)][idy(u)],T))
ins(u,t,DisPP(p[idx(u)][idy(u)],T));
if(can(S,T)) {puts("10.00");continue;}
DAG();
printf("%.2f\n",d[s]);
} return ;
}