POJ3798 Cave Crisis 计算几何+最短路

23 篇文章 0 订阅
18 篇文章 0 订阅

题目让你求一个最大的圆,能从隧道起点到终点,中间有很多障碍。

此题有一个注意点就是终点位置不必满足枚举的距离。判断了就是哇哇哇。还有就是起点到其他边或者多边形的距离除了覆盖接触也不行。

二分枚举圆的半径,然后更具枚举的半径建图求最短路即可


Cave Crisis
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 87 Accepted: 24

Description

R2D2 was exploring a tunnel when a cave-in suddenly occurred. Oh no, is he trapped? 



From an overhead view, we can see all the obstacles (debris) on a two-dimensional Cartesian plane. The tunnel is w cm wide, bounded by the lines y = w/2 and y = -w/2. R2D2 starts at (0, 0), and has a perfectly circular footprint of radius r. The exit of the tunnel lies to the right of the line x = 1000. Between R2D2 and the exit lie a number of polygonal obstacles. 

Is it possible for R2D2 to navigate between the obstacles and make it to the exit?

Input

The input file will contain multiple test cases. Each test case begins with a single line containing an even integer w (2 ≤ w ≤ 1000), the width of the tunnel, and an integer N (0 ≤ N ≤ 100), the number of obstacles. The next N lines each contain the description of a single obstacle. The ith obstacle is a simple polygon, specified on a single line containing an integer ni (3 ≤ ni ≤ 10), the number of vertices, followed by ni pairs of integers, xij and yij (0 ≤ xij ≤ 1000 and -w/2 ≤ yij ≤ w/2 for j = 1, ..., ni ), specifying the coordinates of the vertices in counterclockwise order. Note that obstacles in the input may touch or even overlap. However, you are guaranteed that R2D2’s initial location will not touch or overlap any obstacle. The vertices of each polygon are unique, no two nonconsecutive edges of the polygon intersect (even at their endpoints), and each polygon is guaranteed to have nonzero area. The end-of-input is denoted by an invalid test case with w = N = 0 and should not be processed.

Output

For each input test case, you must determine the maximum radius r > 0 that R2D2 could have and still be able to plan a path from his starting location (0, 0) to the tunnel exit without overlapping with any of the obstacles. You should print either this maximum radius r (rounded to two decimal places) or the message “impossible” if no such radius exists.

Sample Input

6 2 
4 2 -1 4 -1 4 1 2 1 
3 3 0 6 -1 6 1 
8 2 
3 1 -1 4 -1 4 4 
3 3 -4 6 1 3 1 
10 7 
4 0 5 4 2 5 3 4 5 
3 4 -5 9 -5 9 0 
4 8 -5 11 -5 11 -2 8 -2 
3 8 3 16 1 11 5 
4 21 -5 23 -3 20 -2 15 -4 
3 22 3 26 -1 28 0 
3 24 0 29 4 25 3 
0 0

Sample Output

1.00
impossible
1.33
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<queue>

using namespace std;

#define INF 0x3f3f3f3f
const double eps=1e-8;

struct edges
{
    int v,next;
}edge[110*110*110];
int head[110],en,n,cnts[110],dis[110];
double w;
bool in[110];
double g[110][110];
double sd[110],ed[110];

void add(int u,int v)
{
    edge[en].v=v;
    edge[en].next=head[u];
    head[u]=en++;

    edge[en].v=u;
    edge[en].next=head[v];
    head[v]=en++;
}

inline double sqr(double x)
{
    return x*x;
}

int sig(double x)
{
    if(fabs(x)<eps) return 0;
    if(x>0) return 1;
    return -1;
}

struct point
{
    double x,y;
    point(){};
    point(double a,double b):x(a),y(b){}
    void input()
    {
        scanf("%lf%lf",&x,&y);
    }
    friend point operator + (const point &a,const point &b)
    {
        return point(a.x+b.x,a.y+b.y);
    }
    friend point operator - (const point &a,const point &b)
    {
        return point(a.x-b.x,a.y-b.y);
    }
    friend bool operator == (const point &a,const point &b)
    {
        return sig(a.x-b.x)==0 && sig(a.y-b.y)==0;
    }
    friend point operator * (const point &a,const double &b)
    {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (const double &a,const point &b)
    {
        return point(a*b.x,a*b.y);
    }
    friend point operator / (const point &a,const double &b)
    {
        return point(a.x/b,a.y/b);
    }
    double norm()
    {
        return sqrt(sqr(x)+sqr(y));
    }
}p[110][12];


double det(const point &a,const point &b)
{
    return a.x*b.y-a.y*b.x;
}

double dot(const point &a,const point &b)
{
    return a.x*b.x+a.y*b.y;
}

double dist(const point &a,const point &b)
{
    return (a-b).norm();
}


struct line
{
    point a,b;
    line() {}
    line(point x,point y):a(x),b(y){}
};

line point_make_line(const point a,const point b)
{
    return line(a,b);
}

double dis_point_segment(const point p,const point s,const point t)
{
    if(sig(dot(p-s,t-s))<0) return (p-s).norm();
    if(sig(dot(p-t,s-t))<0) return (p-t).norm();
    return fabs(det(s-p,t-p)/dist(s,t));
}

bool PointOnSegment(point p,point s,point t)
{
    return sig(det(p-s,t-s))==0 && sig(dot(p-s,p-t))<=0;
}


double det1(double x1, double y1, double x2, double y2)
{
    return x1 * y2 - x2 * y1;
}

double xmult1(point p0,point p1,point p2)
{
    return det1(p1.x-p0.x,p1.y-p0.y,p2.x-p0.x,p2.y-p0.y);
}

double dotdet1(double x1,double y1,double x2,double y2)
{
    return x1*x2+y1*y2;
}

double dot1(point p0, point p1, point p2)
{
    return dotdet1(p1.x-p0.x,p1.y-p0.y,p2.x-p0.x,p2.y-p0.y);
}

int between1(point p0,point p1, point p2)
{
    return sig(dot1(p0,p1,p2));
}

int intersect1(point a, point b, point c, point d)
{
    double s1, s2, s3, s4;
    int d1 = sig(s1 = xmult1(a, b, c));
    int d2 = sig(s2 = xmult1(a, b, d));
    int d3 = sig(s3 = xmult1(c, d, a));
    int d4 = sig(s4 = xmult1(c, d, b));
    if ((d1^d2) == -2 && (d3^d4) == -2)
   	{
      return 1;
    }
    if (d1 == 0 && between1(c, a, b) <= 0) return 2;
    if (d2 == 0 && between1(d, a, b) <= 0) return 2;
    if (d3 == 0 && between1(a, c, d) <= 0) return 2;
    if (d4 == 0 && between1(b, c, d) <= 0) return 2;
    return 0;
}

double getdis(int a,int b)
{
    double dis=0x3f3f3f3f;
    for(int i=0;i<cnts[a];i++)
        for(int j=0;j<cnts[b];j++)
            if(intersect1(p[a][i],p[a][(i+1)%cnts[a]],p[b][j],p[b][(j+1)%cnts[b]])) return 0;
    for(int i=0;i<cnts[a];i++)
        for(int j=0;j<cnts[b];j++)
            dis=min(dis,dist(p[a][i],p[b][j]));
    for(int i=0;i<cnts[a];i++)
        for(int j=0;j<cnts[b];j++)
            dis=min(dis,dis_point_segment(p[a][i],p[b][j],p[b][(j+1)%cnts[b]]));
    for(int i=0;i<cnts[b];i++)
        for(int j=0;j<cnts[a];j++)
            dis=min(dis,dis_point_segment(p[b][i],p[a][j],p[a][(j+1)%cnts[a]]));
    return dis;
}

void initDis()
{
    point s,e;
    double st=w/2;
    double end=-w/2;
    sd[0]=w/2;sd[n+1]=w/2;
    s.x=0,s.y=0;
    for(int i=1;i<=n;i++)
    {
        double diss1=0x3f3f3f3f;
        for(int j=0;j<cnts[i];j++)

        {
            diss1=min(diss1,dist(s,p[i][j]));
            diss1=min(diss1,dis_point_segment(s,p[i][j],p[i][(j+1)%cnts[i]]));
        }
        sd[i]=diss1;
    }
    for(int i=1;i<=n;i++)
    {
        double disS=0x3f3f3f3f,disE=0x3f3f3f3f;
        for(int j=0;j<cnts[i];j++)
        {
            disS=min(disS,st-p[i][j].y);
            disE=min(disE,p[i][j].y-end);
        }
        g[0][i]=g[i][0]=disS;
        g[i][n+1]=g[n+1][i]=disE;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            double len=getdis(i,j);
            g[i][j]=g[j][i]=len;
        }
    }
    g[0][n+1]=w;
    g[n+1][0]=w;
}

bool build(double r)
{
    memset(head,-1,sizeof(head));en=0;
    for(int i=0;i<=n+1;i++)
        if(sig(sd[i]-r)<=0) return false;
    for(int i=0;i<=n+1;i++)
        for(int j=i+1;j<=n+1;j++)
        {
            if(sig(g[i][j]-2*r)<0)
                add(i,j);
        }
    return true;
}

void spfa(int s)
{
	queue<int> q;
	for(int i=0;i<=n+1;i++)
	{
		dis[i]=INF;
		in[i]=false;
	}
	dis[s]=0;
	in[s]=true;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		in[u]=false;
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			int v=edge[i].v;
			if(dis[u]+1<dis[v])
			{
				dis[v]=dis[u]+1;
				if(!in[v])
				{
					q.push(v);
					in[v]=true;
				}

			}
		}
	}
}

int main()
{
    while(cin>>w>>n)
    {
        if(w==0&&n==0) break;
        for(int i=1;i<=n;i++)
        {
            cin>>cnts[i];
            for(int j=0;j<cnts[i];j++) cin>>p[i][j].x>>p[i][j].y;
        }
        initDis();
        double l=0,r=w,m;
        while(l+eps<r)
        {
            m=(l+r)/2;
            bool fg=build(m);
            if(fg)spfa(0);
           // system("pause");
            if(fg&&dis[n+1]==INF)
                l=m;
            else
                r=m;
        }
        if(l!=0)
            cout<<setprecision(2)<<setiosflags(ios::fixed)<<l<<endl;

        else
            printf("impossible\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值