POJ 1556 The Doors --几何,最短路

时间:2025-01-27 09:36:44

题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少。

解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短路SPFA,即可得出答案。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#define Mod 1000000007
#define eps 1e-8
using namespace std;
#define N 100017 struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
bool operator < (const Line &L)const { return ang < L.ang; }
};
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
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 p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); } double DisP(Point A,Point B){
return Length(B-A);
}
bool SegmentIntersection(Point A,Point B,Point C,Point D) {
if(dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) < && dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) < ) return true;
return false;
}
//data segment
struct node{
Point P[];
}line[];
Point p[];
vector<pair<int,double> > G[];
double dis[];
int vis[],tot,Ltot,S,E;
//data ends void SPFA()
{
for(int i=;i<=tot;i++) dis[i] = Mod;
memset(vis,,sizeof(vis));
queue<int> q;
q.push(S);
vis[S] = , dis[S] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i].first;
double w = G[u][i].second;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if(!vis[v]) { q.push(v), vis[v] = ; }
}
}
}
} int main()
{
int n,i,j,k,h;
double x,a,b,c,d;
while(scanf("%d",&n)!=EOF && n!=-)
{
tot = ,Ltot = ;
p[] = Point(,);
for(i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf%lf",&x,&a,&b,&c,&d);
p[++tot] = Point(x,a);
p[++tot] = Point(x,b);
p[++tot] = Point(x,c);
p[++tot] = Point(x,d);
line[++Ltot].P[] = Point(x,), line[Ltot].P[] = Point(x,a);
line[++Ltot].P[] = Point(x,b), line[Ltot].P[] = Point(x,c);
line[++Ltot].P[] = Point(x,d), line[Ltot].P[] = Point(x,);
}
p[++tot] = Point(,);
Point A,B;
for(i=;i<=tot;i++) G[i].clear();
for(i=;i<=tot;i++) //start
{
for(j=i+;j<=tot;j++) //end
{
A = p[i], B = p[j];
for(k=;k<=Ltot;k++)
{
if(SegmentIntersection(A,B,line[k].P[],line[k].P[]))
break;
}
if(k == Ltot+)
{
G[i].push_back(make_pair(j,DisP(A,B)));
G[j].push_back(make_pair(i,DisP(A,B)));
}
}
}
S = , E = tot;
SPFA();
printf("%.2f\n",dis[E]);
}
return ;
}